node/benchmark/buffers/buffer-bytelength-buffer.js
Joyee Cheung 43ddf7631e
benchmark: split Buffer.byteLength benchmark
PR-URL: https://github.com/nodejs/node/pull/46616
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2023-02-26 19:02:42 +01:00

23 lines
542 B
JavaScript

'use strict';
const common = require('../common');
const bench = common.createBenchmark(main, {
len: [2, 16, 256], // x16
n: [4e6],
});
function main({ n, len }) {
const data = Buffer.alloc(len * 16, 'a');
const expected = Buffer.byteLength(data, 'buffer');
let changed = false;
bench.start();
for (let i = 0; i < n; i++) {
const actual = Buffer.byteLength(data, 'buffer');
if (expected !== actual) { changed = true; }
}
bench.end(n);
if (changed) {
throw new Error('Result changed during iteration');
}
}