From 5b0ce376a2d7fc785b4902f8166cbb54d8fcd4ff Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 25 Nov 2024 18:25:04 +0000 Subject: [PATCH] assert: optimize partial comparison of two `Set`s MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/55970 Reviewed-By: Michaƫl Zasso Reviewed-By: Chemi Atlow Reviewed-By: Luigi Pinca --- 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;