Skip to content

Commit

Permalink
assert: optimize partial comparison of two Sets
Browse files Browse the repository at this point in the history
PR-URL: #55970
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Chemi Atlow <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
aduh95 authored Nov 25, 2024
1 parent b17a1fb commit a43a283
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,33 +414,25 @@ 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
}

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;
Expand Down

0 comments on commit a43a283

Please sign in to comment.