Files
node/test/parallel/test-buffer-bytelength-strings.js
Robert Nagy bf246cf4b5 buffer: limit UTF-8 fast calls to safe one-byte strings
Replace the allocating UTF-8 fast call with a fast binding for
sequential one-byte strings. Its slow fallback only returns a Smi
sentinel. JavaScript sends rejected strings to a separate regular
binding that can flatten strings and handle lazy deoptimization.

Both native callbacks used by the fast API avoid JS-heap allocation.
Add GC regression and string-representation coverage, including
optimized fast acceptance and rejection checks.

Fixes: https://github.com/nodejs/node/issues/66870
Refs: https://github.com/nodejs/node/pull/67134
Assisted-by: Codex
Signed-off-by: Robert Nagy <ronagy@icloud.com>
PR-URL: https://github.com/nodejs/node/pull/67136
Refs: https://github.com/nodejs/node/issues/66870
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
2026-11-01 08:22:55 +00:00

52 lines
1.2 KiB
JavaScript

// Flags: --allow-natives-syntax --expose_externalize_string
'use strict';
const common = require('../common');
const assert = require('assert');
const {
createExternalizableString,
createExternalizableTwoByteString,
externalizeString,
isOneByteString,
} = globalThis;
common.allowGlobals(
createExternalizableString,
createExternalizableTwoByteString,
externalizeString,
isOneByteString,
globalThis.x,
);
const { byteLength } = Buffer;
const latin1 = Buffer.from('aé'.repeat(100), 'latin1').toString('latin1');
const external = createExternalizableString('external é'.repeat(20));
externalizeString(external);
assert.strictEqual(isOneByteString(external), true);
// Check flat, cons, sliced, two-byte and external strings, including lone
// surrogates, before and after optimizing the caller.
const cases = [
'hello',
'é',
latin1,
'x'.repeat(1000) + latin1,
latin1.slice(1, 150),
'€',
'\ud83d',
'a€😀'.repeat(200),
external,
];
function test() {
for (const string of cases) {
assert.strictEqual(byteLength(string), Buffer.from(string).length);
}
}
eval('%PrepareFunctionForOptimization(byteLength)');
test();
eval('%OptimizeFunctionOnNextCall(byteLength)');
test();