From caec80e3ae8cad70e3e78fb6d9403fe7cf94912f Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Sun, 18 Oct 2026 15:15:42 +0300 Subject: [PATCH 1/3] lib: optimize TextEncoder.encode for short strings Assisted-by: Claude opus 5.5 Signed-off-by: Mert Can Altin --- lib/internal/encoding.js | 18 +++++++- ...hatwg-encoding-custom-textencoder-short.js | 42 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-whatwg-encoding-custom-textencoder-short.js diff --git a/lib/internal/encoding.js b/lib/internal/encoding.js index 11003db8bc51..2ad74d54536d 100644 --- a/lib/internal/encoding.js +++ b/lib/internal/encoding.js @@ -13,6 +13,7 @@ const { StringPrototypeSlice, Symbol, SymbolToStringTag, + Uint8Array, } = primordials; const { FastBuffer } = require('internal/buffer'); @@ -338,11 +339,26 @@ function getEncodingFromLabel(label) { let lazyInspect; +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) { diff --git a/test/parallel/test-whatwg-encoding-custom-textencoder-short.js b/test/parallel/test-whatwg-encoding-custom-textencoder-short.js new file mode 100644 index 000000000000..136edfed9d99 --- /dev/null +++ b/test/parallel/test-whatwg-encoding-custom-textencoder-short.js @@ -0,0 +1,42 @@ +'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); +} + +{ + // Results must not share memory with each other. + const first = encoder.encode('first'); + const second = encoder.encode('second'); + assert.notStrictEqual(first.buffer, second.buffer); + assert.strictEqual(Buffer.from(first).toString(), 'first'); + assert.strictEqual(Buffer.from(second).toString(), 'second'); +} From a13f524ba1df1f2c288f3ad5ba3112390c346bff Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Sun, 18 Oct 2026 16:51:17 +0300 Subject: [PATCH 2/3] Update test/parallel/test-whatwg-encoding-custom-textencoder-short.js Co-authored-by: Antoine du Hamel --- .../test-whatwg-encoding-custom-textencoder-short.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/test/parallel/test-whatwg-encoding-custom-textencoder-short.js b/test/parallel/test-whatwg-encoding-custom-textencoder-short.js index 136edfed9d99..fb5774b78c94 100644 --- a/test/parallel/test-whatwg-encoding-custom-textencoder-short.js +++ b/test/parallel/test-whatwg-encoding-custom-textencoder-short.js @@ -32,11 +32,3 @@ for (const input of inputs) { assert.deepStrictEqual(Buffer.from(result), expected); } -{ - // Results must not share memory with each other. - const first = encoder.encode('first'); - const second = encoder.encode('second'); - assert.notStrictEqual(first.buffer, second.buffer); - assert.strictEqual(Buffer.from(first).toString(), 'first'); - assert.strictEqual(Buffer.from(second).toString(), 'second'); -} From 5f342bdc9e6f59da47b3a8dea92efb1953418e5a Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Sun, 18 Oct 2026 16:58:00 +0300 Subject: [PATCH 3/3] lint --- test/parallel/test-whatwg-encoding-custom-textencoder-short.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/parallel/test-whatwg-encoding-custom-textencoder-short.js b/test/parallel/test-whatwg-encoding-custom-textencoder-short.js index fb5774b78c94..3f8ad64b5673 100644 --- a/test/parallel/test-whatwg-encoding-custom-textencoder-short.js +++ b/test/parallel/test-whatwg-encoding-custom-textencoder-short.js @@ -31,4 +31,3 @@ for (const input of inputs) { assert.strictEqual(result.buffer.byteLength, expected.length); assert.deepStrictEqual(Buffer.from(result), expected); } -