mirror of
https://github.com/nodejs/node.git
synced 2026-10-28 10:15:07 +08:00
lib: optimize TextEncoder.encode for short strings
Assisted-by: Claude opus 5.5 Signed-off-by: Mert Can Altin <mertgold60@gmail.com> PR-URL: https://github.com/nodejs/node/pull/66831 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Robert Nagy <ronagy@icloud.com>
This commit is contained in:
1 parent
08b09a2f62
commit
6353f4026c
2 files changed
+51
-1
No files matched your search
@@ -13,6 +13,7 @@ const {
|
||||
StringPrototypeSlice,
|
||||
Symbol,
|
||||
SymbolToStringTag,
|
||||
Uint8Array,
|
||||
} = primordials;
|
||||
|
||||
const { FastBuffer } = require('internal/buffer');
|
||||
@@ -338,11 +339,27 @@ function getEncodingFromLabel(label) {
|
||||
|
||||
let lazyInspect;
|
||||
|
||||
// Maximum string length handled by the reusable scratch buffer.
|
||||
const kSmallEncodeLength = 32;
|
||||
let smallEncodeScratch;
|
||||
|
||||
class TextEncoder {
|
||||
#encoding = 'utf-8';
|
||||
|
||||
#encode(input) {
|
||||
return encodeUtf8String(`${input}`);
|
||||
input = `${input}`;
|
||||
if (input.length > kSmallEncodeLength) {
|
||||
return encodeUtf8String(input);
|
||||
}
|
||||
|
||||
smallEncodeScratch ??= new Uint8Array(kSmallEncodeLength * 3);
|
||||
encodeInto(input, smallEncodeScratch);
|
||||
const written = encodeIntoResults[1];
|
||||
const result = new Uint8Array(written);
|
||||
for (let i = 0; i < written; i++) {
|
||||
result[i] = smallEncodeScratch[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
#encodeInto(input, dest) {
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
'use strict';
|
||||
|
||||
// Tests TextEncoder.prototype.encode() around the short string path.
|
||||
|
||||
require('../common');
|
||||
|
||||
const assert = require('assert');
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
const inputs = [
|
||||
'',
|
||||
'a',
|
||||
'a'.repeat(32),
|
||||
'a'.repeat(33),
|
||||
'\xff'.repeat(32),
|
||||
'ğ'.repeat(32),
|
||||
'ࠀ'.repeat(32),
|
||||
'\u{1F44B}'.repeat(16),
|
||||
'\u{1F44B}'.repeat(17),
|
||||
'\ud800',
|
||||
'a\udc00b',
|
||||
'abc\ud83d',
|
||||
];
|
||||
|
||||
for (const input of inputs) {
|
||||
const result = encoder.encode(input);
|
||||
const expected = Buffer.from(input, 'utf8');
|
||||
assert.strictEqual(Object.getPrototypeOf(result), Uint8Array.prototype);
|
||||
assert.strictEqual(result.byteOffset, 0);
|
||||
assert.strictEqual(result.buffer.byteLength, expected.length);
|
||||
assert.deepStrictEqual(Buffer.from(result), expected);
|
||||
}
|
||||
Reference in new issue
Block a user