mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
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:
1 file changed
+5
-13
+5
-13
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user