summaryrefslogtreecommitdiff
path: root/cli/js/web/text_encoding.ts
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-03-19 21:31:56 +0100
committerGitHub <noreply@github.com>2020-03-19 21:31:56 +0100
commit87d2ba42bf0dedcd91059145bf8ab5941236354b (patch)
tree52ad0906f8b41586cf9fb57ce02646100725b0c6 /cli/js/web/text_encoding.ts
parent392d2c11182332b8d3c168169b1585e3419cb1eb (diff)
perf: Optimize TextEncoder and TextDecoder (#4430)
* add tests for "Deno.core.encode" and "Deno.core.decode" for empty inputs * use "Deno.core.encode" in "TextEncoder" * use "Deno.core.decode" in "TextDecoder" * remove "core_decode" and "core_encode" benchmarks
Diffstat (limited to 'cli/js/web/text_encoding.ts')
-rw-r--r--cli/js/web/text_encoding.ts16
1 files changed, 12 insertions, 4 deletions
diff --git a/cli/js/web/text_encoding.ts b/cli/js/web/text_encoding.ts
index 2da53d934..5f04972aa 100644
--- a/cli/js/web/text_encoding.ts
+++ b/cli/js/web/text_encoding.ts
@@ -26,7 +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 { core } from "../core.ts";
const CONTINUE = null;
const END_OF_STREAM = -1;
@@ -352,6 +352,15 @@ export class TextDecoder {
bytes = new Uint8Array(0);
}
+ // For simple utf-8 decoding "Deno.core.decode" can be used for performance
+ if (
+ this._encoding === "utf-8" &&
+ this.fatal === false &&
+ this.ignoreBOM === false
+ ) {
+ return core.decode(bytes);
+ }
+
// For performance reasons we utilise a highly optimised decoder instead of
// the general decoder.
if (this._encoding === "utf-8") {
@@ -396,10 +405,9 @@ interface TextEncoderEncodeIntoResult {
export class TextEncoder {
readonly encoding = "utf-8";
encode(input = ""): Uint8Array {
- // For performance reasons we utilise a highly optimised decoder instead of
- // the general decoder.
+ // Deno.core.encode() provides very efficient utf-8 encoding
if (this.encoding === "utf-8") {
- return encodeUtf8(input);
+ return core.encode(input);
}
const encoder = new UTF8Encoder();