mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00
assert,util: improve array comparison
Sparse arrays and arrays containing undefined are now compared faster using assert.deepStrictEqual() or util.isDeepStrictEqual(). PR-URL: https://github.com/nodejs/node/pull/57619 Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Edy Silva <edigleyssonsilva@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
parent
4abad0763e
commit
b9c9bf4945
3 changed files with 34 additions and 26 deletions
|
@ -11,6 +11,8 @@ const bench = common.createBenchmark(main, {
|
|||
method: [
|
||||
'deepEqual_Array',
|
||||
'notDeepEqual_Array',
|
||||
'deepEqual_sparseArray',
|
||||
'notDeepEqual_sparseArray',
|
||||
'deepEqual_Set',
|
||||
'notDeepEqual_Set',
|
||||
],
|
||||
|
@ -25,18 +27,30 @@ function run(fn, n, actual, expected) {
|
|||
}
|
||||
|
||||
function main({ n, len, method, strict }) {
|
||||
const actual = [];
|
||||
const expected = [];
|
||||
let actual = Array.from({ length: len }, (_, i) => i);
|
||||
// Contain one undefined value to trigger a specific code path
|
||||
actual[0] = undefined;
|
||||
let expected = actual.slice(0);
|
||||
|
||||
for (let i = 0; i < len; i++) {
|
||||
actual.push(i);
|
||||
expected.push(i);
|
||||
}
|
||||
if (method.includes('not')) {
|
||||
expected[len - 1] += 1;
|
||||
}
|
||||
|
||||
switch (method) {
|
||||
case 'deepEqual_sparseArray':
|
||||
case 'notDeepEqual_sparseArray':
|
||||
actual = new Array(len);
|
||||
for (let i = 0; i < len; i += 2) {
|
||||
actual[i] = i;
|
||||
}
|
||||
expected = actual.slice(0);
|
||||
if (method.includes('not')) {
|
||||
expected[len - 2] += 1;
|
||||
run(strict ? notDeepStrictEqual : notDeepEqual, n, actual, expected);
|
||||
} else {
|
||||
run(strict ? deepStrictEqual : deepEqual, n, actual, expected);
|
||||
}
|
||||
break;
|
||||
case 'deepEqual_Array':
|
||||
run(strict ? deepStrictEqual : deepEqual, n, actual, expected);
|
||||
break;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue