perf: TextEncoder.encode improvement (#3596)

This commit is contained in:
Luca Casonato 2020-01-04 23:00:03 +01:00 committed by Ry Dahl
parent 0a900949c8
commit c41280a057
2 changed files with 87 additions and 0 deletions

View file

@ -26,6 +26,7 @@
import * as base64 from "./base64.ts";
import { decodeUtf8 } from "./decode_utf8.ts";
import * as domTypes from "./dom_types.ts";
import { encodeUtf8 } from "./encode_utf8.ts";
import { DenoError, ErrorKind } from "./errors.ts";
const CONTINUE = null;
@ -405,6 +406,12 @@ export class TextEncoder {
readonly encoding = "utf-8";
/** Returns the result of running UTF-8's encoder. */
encode(input = ""): Uint8Array {
// For performance reasons we utilise a highly optimised decoder instead of
// the general decoder.
if (this.encoding === "utf-8") {
return encodeUtf8(input);
}
const encoder = new UTF8Encoder();
const inputStream = new Stream(stringToCodePoints(input));
const output: number[] = [];