Skip to content

Commit 686deb7

Browse files
committed
assert,util: improve array comparison
Sparse arrays and arrays containing undefined are now compared faster using assert.deepStrictEqual() or util.isDeepStrictEqual().
1 parent a4f556f commit 686deb7

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

benchmark/assert/deepequal-simple-array-and-set.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const bench = common.createBenchmark(main, {
1111
method: [
1212
'deepEqual_Array',
1313
'notDeepEqual_Array',
14+
'deepEqual_sparseArray',
15+
'notDeepEqual_sparseArray',
1416
'deepEqual_Set',
1517
'notDeepEqual_Set',
1618
],
@@ -25,18 +27,30 @@ function run(fn, n, actual, expected) {
2527
}
2628

2729
function main({ n, len, method, strict }) {
28-
const actual = [];
29-
const expected = [];
30+
let actual = Array.from({ length: len }, (_, i) => i);
31+
// Contain one undefined value to trigger a specific code path
32+
actual[0] = undefined;
33+
let expected = actual.slice(0);
3034

31-
for (let i = 0; i < len; i++) {
32-
actual.push(i);
33-
expected.push(i);
34-
}
3535
if (method.includes('not')) {
3636
expected[len - 1] += 1;
3737
}
3838

3939
switch (method) {
40+
case 'deepEqual_sparseArray':
41+
case 'notDeepEqual_sparseArray':
42+
actual = new Array(len);
43+
for (let i = 0; i < len; i += 2) {
44+
actual[i] = i;
45+
}
46+
expected = actual.slice(0);
47+
if (method.includes('not')) {
48+
expected[len - 2] += 1;
49+
run(strict ? notDeepStrictEqual : notDeepEqual, n, actual, expected);
50+
} else {
51+
run(strict ? deepStrictEqual : deepEqual, n, actual, expected);
52+
}
53+
break;
4054
case 'deepEqual_Array':
4155
run(strict ? deepStrictEqual : deepEqual, n, actual, expected);
4256
break;

lib/internal/assert/myers_diff.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function myersDiff(actual, expected, checkCommaDisparity = false) {
2929
const actualLength = actual.length;
3030
const expectedLength = expected.length;
3131
const max = actualLength + expectedLength;
32+
// TODO(BridgeAR): Cap the input in case the values go beyond the limit of 2^31 - 1.
3233
const v = new Int32Array(2 * max + 1);
3334
const trace = [];
3435

lib/internal/util/comparisons.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,9 @@ function innerDeepEqual(val1, val2, mode, memos) {
196196
}
197197
} else {
198198
if (val1 === null || typeof val1 !== 'object') {
199-
if (val2 === null || typeof val2 !== 'object') {
200-
// eslint-disable-next-line eqeqeq
201-
return val1 == val2 || (NumberIsNaN(val1) && NumberIsNaN(val2));
202-
}
203-
return false;
199+
return (val2 === null || typeof val2 !== 'object') &&
200+
// eslint-disable-next-line eqeqeq
201+
(val1 == val2 || (NumberIsNaN(val1) && NumberIsNaN(val2)));
204202
}
205203
if (val2 === null || typeof val2 !== 'object') {
206204
return false;
@@ -384,9 +382,7 @@ function keyCheck(val1, val2, mode, memos, iterationType, keys2) {
384382
}
385383
} else if (keys2.length !== ObjectKeys(val1).length) {
386384
return false;
387-
}
388-
389-
if (mode === kStrict) {
385+
} else if (mode === kStrict) {
390386
const symbolKeysA = getOwnSymbols(val1);
391387
if (symbolKeysA.length !== 0) {
392388
let count = 0;
@@ -758,9 +754,9 @@ function sparseArrayEquiv(a, b, mode, memos, i) {
758754
if (keysA.length !== keysB.length) {
759755
return false;
760756
}
761-
for (; i < keysA.length; i++) {
762-
const key = keysA[i];
763-
if (!hasOwn(b, key) || !innerDeepEqual(a[key], b[key], mode, memos)) {
757+
for (; i < keysB.length; i++) {
758+
const key = keysB[i];
759+
if ((a[key] === undefined && !hasOwn(a, key)) || !innerDeepEqual(a[key], b[key], mode, memos)) {
764760
return false;
765761
}
766762
}
@@ -782,17 +778,14 @@ function objEquiv(a, b, mode, keys2, memos, iterationType) {
782778
return partialArrayEquiv(a, b, mode, memos);
783779
}
784780
for (let i = 0; i < a.length; i++) {
785-
if (!innerDeepEqual(a[i], b[i], mode, memos)) {
786-
return false;
787-
}
788-
const isSparseA = a[i] === undefined && !hasOwn(a, i);
789-
const isSparseB = b[i] === undefined && !hasOwn(b, i);
790-
if (isSparseA !== isSparseB) {
781+
if (b[i] === undefined) {
782+
if (!hasOwn(b, i))
783+
return sparseArrayEquiv(a, b, mode, memos, i);
784+
if (a[i] !== undefined || !hasOwn(a, i))
785+
return false;
786+
} else if (a[i] === undefined || !innerDeepEqual(a[i], b[i], mode, memos)) {
791787
return false;
792788
}
793-
if (isSparseA) {
794-
return sparseArrayEquiv(a, b, mode, memos, i);
795-
}
796789
}
797790
} else if (iterationType === kIsSet) {
798791
if (!setEquiv(a, b, mode, memos)) {

0 commit comments

Comments
 (0)