nodejs/benchmark/assert/deepequal-buffer.js
Ruben Bridgewater 1fbe3351ba
assert: improve partialDeepStrictEqual performance
This implements fast paths for typed arrays, array buffers and sets
and maps that contain only objects as keys.

PR-URL: https://github.com/nodejs/node/pull/57509
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
2025-03-19 23:50:56 +00:00

54 lines
1.2 KiB
JavaScript

'use strict';
const common = require('../common.js');
const assert = require('assert');
const bench = common.createBenchmark(main, {
n: [2e4],
len: [1e2, 1e3],
strict: [0, 1],
arrayBuffer: [0, 1],
method: ['deepEqual', 'notDeepEqual', 'unequal_length', 'partial'],
}, {
combinationFilter: (p) => {
return p.strict === 1 || p.method === 'deepEqual';
},
});
function main({ len, n, method, strict, arrayBuffer }) {
let actual = Buffer.alloc(len);
let expected = Buffer.alloc(len + Number(method === 'unequal_length'));
if (method === 'unequal_length') {
method = 'notDeepEqual';
}
if (method === 'partial') {
method = 'partialDeepStrictEqual';
} else if (strict) {
method = method.replace('eep', 'eepStrict');
}
for (let i = 0; i < len; i++) {
actual.writeInt8(i % 128, i);
expected.writeInt8(i % 128, i);
}
if (method.includes('not')) {
const position = Math.floor(len / 2);
expected[position] = expected[position] + 1;
}
const fn = assert[method];
if (arrayBuffer) {
actual = actual.buffer;
expected = expected.buffer;
}
bench.start();
for (let i = 0; i < n; ++i) {
fn(actual, expected);
}
bench.end(n);
}