assert: optimize partial comparison of two Sets

PR-URL: https://github.com/nodejs/node/pull/55970
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Antoine du Hamel committed 2024-11-26 15:34:32 +01:00
1 parent a3f7db6b6d
commit 5b0ce376a2
1 file changed
+5 -13
+5 -13
View File
@@ -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;