mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 20:29:11 +00:00
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
This commit is contained in:
parent
392d2c1118
commit
87d2ba42bf
7 changed files with 32 additions and 156 deletions
|
@ -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();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue