mirror of
https://github.com/nodejs/node.git
synced 2026-10-29 02:35:20 +08:00
Signed-off-by: James M Snel <jasnell@gmail.com> Assisted-by: Opencode PR-URL: https://github.com/nodejs/node/pull/65606 Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
38 lines
918 B
JavaScript
38 lines
918 B
JavaScript
'use strict';
|
|
|
|
const { bench } = require('node:bench');
|
|
const path = require('node:path');
|
|
|
|
const methods = ['offset', 'slice'];
|
|
const sizes = [16, 512, 4096, 16386];
|
|
const n = 1e6;
|
|
const name = path.join('buffers', 'buffer-compare-offset.js');
|
|
|
|
function compareUsingSlice(b0, b1, len, iterations) {
|
|
for (let i = 0; i < iterations; i++)
|
|
Buffer.compare(b0.slice(1, len), b1.slice(1, len));
|
|
}
|
|
|
|
function compareUsingOffset(b0, b1, len, iterations) {
|
|
for (let i = 0; i < iterations; i++)
|
|
b0.compare(b1, 1, len, 1, len);
|
|
}
|
|
|
|
for (const method of methods) {
|
|
for (const size of sizes) {
|
|
const compare = method === 'slice' ?
|
|
compareUsingSlice : compareUsingOffset;
|
|
|
|
bench(name, {
|
|
params: { method, n, size },
|
|
}, (b) => {
|
|
b.start();
|
|
compare(Buffer.alloc(size, 'a'),
|
|
Buffer.alloc(size, 'b'),
|
|
size >> 1,
|
|
n);
|
|
b.end(n);
|
|
});
|
|
}
|
|
}
|