Skip to content

Commit 8905518

Browse files
committed
assert,util: fix sparse array comparison
Comparing sparse arrays did not work properly. That is fixed and tests were added to verify that everything works as expected. This had an impact on `util.isDeepStrictEqual()` and `assert.deepStrictEqual()` and their counterpart `assert.notDeepStrictEqual()`. PR-URL: #24749 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 7fb8d31 commit 8905518

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/internal/util/comparisons.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -558,11 +558,10 @@ function objEquiv(a, b, strict, keys, memos, iterationType) {
558558
} else {
559559
// Array is sparse.
560560
const keysA = objectKeys(a);
561-
i++;
562561
for (; i < keysA.length; i++) {
563562
const key = keysA[i];
564563
if (!hasOwnProperty(b, key) ||
565-
!innerDeepEqual(a[key], b[i], strict, memos)) {
564+
!innerDeepEqual(a[key], b[key], strict, memos)) {
566565
return false;
567566
}
568567
}

test/parallel/test-assert-deep.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -571,9 +571,20 @@ assertNotDeepOrStrict(
571571
assertDeepAndStrictEqual(m3, m4);
572572
}
573573

574-
// Handle sparse arrays
575-
assertDeepAndStrictEqual([1, , , 3], [1, , , 3]);
576-
assertOnlyDeepEqual([1, , , 3], [1, , , 3, , , ]);
574+
// Handle sparse arrays.
575+
{
576+
assertDeepAndStrictEqual([1, , , 3], [1, , , 3]);
577+
assertOnlyDeepEqual([1, , , 3], [1, , , 3, , , ]);
578+
const a = new Array(3);
579+
const b = new Array(3);
580+
a[2] = true;
581+
b[1] = true;
582+
assertNotDeepOrStrict(a, b);
583+
b[2] = true;
584+
assertNotDeepOrStrict(a, b);
585+
a[0] = true;
586+
assertNotDeepOrStrict(a, b);
587+
}
577588

578589
// Handle different error messages
579590
{

0 commit comments

Comments
 (0)