From 7174dc2e8a5f4486ebc795eb06a184b1bf7445b1 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sat, 19 Aug 2017 02:47:27 -0300 Subject: [PATCH] assert: handle sparse arrays in deepStrictEqual MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Detect sparse array ends and add a fail early path for unequal array length. PR-URL: https://github.com/nodejs/node/pull/15027 Reviewed-By: Rich Trott Reviewed-By: Yuta Hiroto Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Tobias Nießen --- lib/assert.js | 9 ++++++++- test/parallel/test-assert-deep.js | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/assert.js b/lib/assert.js index 6e4b9effbe5549..e120a981346640 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -178,7 +178,14 @@ function strictDeepEqual(actual, expected) { if (Object.getPrototypeOf(actual) !== Object.getPrototypeOf(expected)) { return false; } - if (isObjectOrArrayTag(actualTag)) { + if (actualTag === '[object Array]') { + // Check for sparse arrays and general fast path + if (actual.length !== expected.length) + return false; + // Skip testing the part below and continue in the callee function. + return; + } + if (actualTag === '[object Object]') { // Skip testing the part below and continue in the callee function. return; } diff --git a/test/parallel/test-assert-deep.js b/test/parallel/test-assert-deep.js index c240d1a33efd9f..068950a33d3c39 100644 --- a/test/parallel/test-assert-deep.js +++ b/test/parallel/test-assert-deep.js @@ -404,4 +404,7 @@ assertOnlyDeepEqual( assertDeepAndStrictEqual(m3, m4); } +assertDeepAndStrictEqual([1, , , 3], [1, , , 3]); +assertOnlyDeepEqual([1, , , 3], [1, , , 3, , , ]); + /* eslint-enable */