Skip to content

Commit 7174dc2

Browse files
BridgeARMylesBorins
authored andcommitted
assert: handle sparse arrays in deepStrictEqual
Detect sparse array ends and add a fail early path for unequal array length. PR-URL: #15027 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent bc250a1 commit 7174dc2

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/assert.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,14 @@ function strictDeepEqual(actual, expected) {
178178
if (Object.getPrototypeOf(actual) !== Object.getPrototypeOf(expected)) {
179179
return false;
180180
}
181-
if (isObjectOrArrayTag(actualTag)) {
181+
if (actualTag === '[object Array]') {
182+
// Check for sparse arrays and general fast path
183+
if (actual.length !== expected.length)
184+
return false;
185+
// Skip testing the part below and continue in the callee function.
186+
return;
187+
}
188+
if (actualTag === '[object Object]') {
182189
// Skip testing the part below and continue in the callee function.
183190
return;
184191
}

test/parallel/test-assert-deep.js

+3
Original file line numberDiff line numberDiff line change
@@ -404,4 +404,7 @@ assertOnlyDeepEqual(
404404
assertDeepAndStrictEqual(m3, m4);
405405
}
406406

407+
assertDeepAndStrictEqual([1, , , 3], [1, , , 3]);
408+
assertOnlyDeepEqual([1, , , 3], [1, , , 3, , , ]);
409+
407410
/* eslint-enable */

0 commit comments

Comments
 (0)