Files
node/benchmark/buffers/buffer-stringlength.js
Matteo Collina 22ab7d77f1 buffer: add Buffer.stringLength()
Add `Buffer.stringLength(input[, encoding])`, the counterpart of
`Buffer.byteLength()`: it returns the number of UTF-16 code units that
`buf.toString(encoding)` would produce, without decoding.

For UTF-8 the count is computed with simdutf. Invalid input is counted
with the same maximal-subpart replacement that the decoder applies, so
the result always matches `toString().length`. The other encodings are
computed from `byteLength` alone.

This lets code that accumulates streamed input check the result against
`buffer.constants.MAX_STRING_LENGTH` and size its memory budget before
decoding.

Refs: https://github.com/nodejs/node/issues/66062
Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/66064
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
2026-09-19 05:21:30 +00:00

29 lines
769 B
JavaScript

'use strict';
const common = require('../common.js');
const { Buffer } = require('node:buffer');
const assert = require('node:assert');
const bench = common.createBenchmark(main, {
n: [1e6],
encoding: ['utf8', 'latin1', 'base64'],
len: [32, 4096, 1048576],
input: ['ascii', 'multibyte', 'invalid'],
});
function main({ n, encoding, len, input }) {
let buf;
if (input === 'ascii') {
buf = Buffer.alloc(len, 'a');
} else {
buf = Buffer.alloc(len - (len % 3), '€');
if (input === 'invalid') buf = Buffer.concat([buf, Buffer.from([0xE2, 0x82])]);
}
const expected = buf.toString(encoding).length;
bench.start();
for (let i = 0; i < n; ++i) {
assert.strictEqual(Buffer.stringLength(buf, encoding), expected);
}
bench.end(n);
}