|
1 | | -/* eslint-disable @typescript-eslint/no-unnecessary-condition */ |
2 | | -import { UINT32_MAX } from "./int"; |
3 | 1 |
|
4 | 2 | export function utf8Count(str: string): number { |
5 | 3 | const strLength = str.length; |
@@ -88,9 +86,14 @@ export function utf8EncodeJs(str: string, output: Uint8Array, outputOffset: numb |
88 | 86 | // https://encoding.spec.whatwg.org/ |
89 | 87 | // and available in all the modern browsers: |
90 | 88 | // https://caniuse.com/textencoder |
| 89 | +// They are available in Node.js since v12 LTS as well: |
| 90 | +// https://nodejs.org/api/globals.html#textencoder |
91 | 91 |
|
92 | 92 | const sharedTextEncoder = new TextEncoder(); |
93 | | -const TEXT_ENCODER_THRESHOLD = 200; |
| 93 | + |
| 94 | +// This threshold should be determined by benchmarking, which might vary in engines and input data. |
| 95 | +// Run `npx ts-node benchmark/encode-string.ts` for details. |
| 96 | +const TEXT_ENCODER_THRESHOLD = 50; |
94 | 97 |
|
95 | 98 | export function utf8EncodeTE(str: string, output: Uint8Array, outputOffset: number): void { |
96 | 99 | sharedTextEncoder.encodeInto(str, output.subarray(outputOffset)); |
@@ -156,6 +159,9 @@ export function utf8DecodeJs(bytes: Uint8Array, inputOffset: number, byteLength: |
156 | 159 | } |
157 | 160 |
|
158 | 161 | const sharedTextDecoder = new TextDecoder(); |
| 162 | + |
| 163 | +// This threshold should be determined by benchmarking, which might vary in engines and input data. |
| 164 | +// Run `npx ts-node benchmark/decode-string.ts` for details. |
159 | 165 | const TEXT_DECODER_THRESHOLD = 200; |
160 | 166 |
|
161 | 167 | export function utf8DecodeTD(bytes: Uint8Array, inputOffset: number, byteLength: number): string { |
|
0 commit comments