From 9bc35119c2b85ca400265bebf0530a3f03614802 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 23 Nov 2024 19:07:40 +0100 Subject: [PATCH] assert: optimize partial comparison of two `Set`s --- lib/assert.js | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index cef7c16f8095e3..3e212a1c3aebbe 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -414,7 +414,6 @@ function compareBranch( } // Check for Set object equality - // TODO(aduh95): switch to `SetPrototypeIsSubsetOf` when it's available if (isSet(actual) && isSet(expected)) { if (expected.size > actual.size) { return false; // `expected` can't be a subset if it has more elements @@ -422,25 +421,18 @@ function compareBranch( if (isDeepEqual === undefined) lazyLoadComparison(); - const actualArray = ArrayFrom(actual); - const expectedArray = ArrayFrom(expected); + const actualArray = ArrayFrom(FunctionPrototypeCall(SafeSet.prototype[SymbolIterator], actual)); + const expectedIterator = FunctionPrototypeCall(SafeSet.prototype[SymbolIterator], expected); const usedIndices = new SafeSet(); - for (let expectedIdx = 0; expectedIdx < expectedArray.length; expectedIdx++) { - const expectedItem = expectedArray[expectedIdx]; - let found = false; - + expectedIteration: for (const expectedItem of expectedIterator) { for (let actualIdx = 0; actualIdx < actualArray.length; actualIdx++) { if (!usedIndices.has(actualIdx) && isDeepStrictEqual(actualArray[actualIdx], expectedItem)) { usedIndices.add(actualIdx); - found = true; - break; + continue expectedIteration; } } - - if (!found) { - return false; - } + return false; } return true;