Skip to content

Commit 4381100

Browse files
committed
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 <rtrott@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 7854562 commit 4381100

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/assert.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,14 @@ function strictDeepEqual(actual, expected) {
180180
if (Object.getPrototypeOf(actual) !== Object.getPrototypeOf(expected)) {
181181
return false;
182182
}
183-
if (isObjectOrArrayTag(actualTag)) {
183+
if (actualTag === '[object Array]') {
184+
// Check for sparse arrays and general fast path
185+
if (actual.length !== expected.length)
186+
return false;
187+
// Skip testing the part below and continue in the callee function.
188+
return;
189+
}
190+
if (actualTag === '[object Object]') {
184191
// Skip testing the part below and continue in the callee function.
185192
return;
186193
}

test/parallel/test-assert-deep.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,4 +466,7 @@ assertOnlyDeepEqual(
466466
assertDeepAndStrictEqual(m3, m4);
467467
}
468468

469+
assertDeepAndStrictEqual([1, , , 3], [1, , , 3]);
470+
assertOnlyDeepEqual([1, , , 3], [1, , , 3, , , ]);
471+
469472
/* eslint-enable */

0 commit comments

Comments
 (0)