Files
node/test/parallel/test-whatwg-encoding-custom-textencoder-short.js
Mert Can Altin 6353f4026c 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>
2026-10-23 07:51:50 +00:00

34 lines
763 B
JavaScript

'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);
}