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:
Ruben Bridgewater 2025-03-25 15:27:50 +01:00 committed by Node.js GitHub Bot
parent 4abad0763e
commit b9c9bf4945
3 changed files with 34 additions and 26 deletions

View file

@ -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;

View file

@ -29,6 +29,7 @@ function myersDiff(actual, expected, checkCommaDisparity = false) {
const actualLength = actual.length;
const expectedLength = expected.length;
const max = actualLength + expectedLength;
// TODO(BridgeAR): Cap the input in case the values go beyond the limit of 2^31 - 1.
const v = new Int32Array(2 * max + 1);
const trace = [];

View file

@ -196,11 +196,9 @@ function innerDeepEqual(val1, val2, mode, memos) {
}
} else {
if (val1 === null || typeof val1 !== 'object') {
if (val2 === null || typeof val2 !== 'object') {
// eslint-disable-next-line eqeqeq
return val1 == val2 || (NumberIsNaN(val1) && NumberIsNaN(val2));
}
return false;
return (val2 === null || typeof val2 !== 'object') &&
// eslint-disable-next-line eqeqeq
(val1 == val2 || (NumberIsNaN(val1) && NumberIsNaN(val2)));
}
if (val2 === null || typeof val2 !== 'object') {
return false;
@ -384,9 +382,7 @@ function keyCheck(val1, val2, mode, memos, iterationType, keys2) {
}
} else if (keys2.length !== ObjectKeys(val1).length) {
return false;
}
if (mode === kStrict) {
} else if (mode === kStrict) {
const symbolKeysA = getOwnSymbols(val1);
if (symbolKeysA.length !== 0) {
let count = 0;
@ -761,9 +757,9 @@ function sparseArrayEquiv(a, b, mode, memos, i) {
if (keysA.length !== keysB.length) {
return false;
}
for (; i < keysA.length; i++) {
const key = keysA[i];
if (!hasOwn(b, key) || !innerDeepEqual(a[key], b[key], mode, memos)) {
for (; i < keysB.length; i++) {
const key = keysB[i];
if ((a[key] === undefined && !hasOwn(a, key)) || !innerDeepEqual(a[key], b[key], mode, memos)) {
return false;
}
}
@ -785,17 +781,14 @@ function objEquiv(a, b, mode, keys2, memos, iterationType) {
return partialArrayEquiv(a, b, mode, memos);
}
for (let i = 0; i < a.length; i++) {
if (!innerDeepEqual(a[i], b[i], mode, memos)) {
if (b[i] === undefined) {
if (!hasOwn(b, i))
return sparseArrayEquiv(a, b, mode, memos, i);
if (a[i] !== undefined || !hasOwn(a, i))
return false;
} else if (a[i] === undefined || !innerDeepEqual(a[i], b[i], mode, memos)) {
return false;
}
const isSparseA = a[i] === undefined && !hasOwn(a, i);
const isSparseB = b[i] === undefined && !hasOwn(b, i);
if (isSparseA !== isSparseB) {
return false;
}
if (isSparseA) {
return sparseArrayEquiv(a, b, mode, memos, i);
}
}
} else if (iterationType === kIsSet) {
if (!setEquiv(a, b, mode, memos)) {