Skip to content

Commit 5b0ce37

Browse files
committed
assert: optimize partial comparison of two Sets
PR-URL: #55970 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent a3f7db6 commit 5b0ce37

1 file changed

Lines changed: 5 additions & 13 deletions

File tree

‎lib/assert.js‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -414,33 +414,25 @@ function compareBranch(
414414
}
415415

416416
// Check for Set object equality
417-
// TODO(aduh95): switch to `SetPrototypeIsSubsetOf` when it's available
418417
if(isSet(actual)&&isSet(expected)){
419418
if(expected.size>actual.size){
420419
returnfalse;// `expected` can't be a subset if it has more elements
421420
}
422421

423422
if(isDeepEqual===undefined)lazyLoadComparison();
424423

425-
constactualArray=ArrayFrom(actual);
426-
constexpectedArray=ArrayFrom(expected);
424+
constactualArray=ArrayFrom(FunctionPrototypeCall(SafeSet.prototype[SymbolIterator],actual));
425+
constexpectedIterator=FunctionPrototypeCall(SafeSet.prototype[SymbolIterator],expected);
427426
constusedIndices=newSafeSet();
428427

429-
for(letexpectedIdx=0;expectedIdx<expectedArray.length;expectedIdx++){
430-
constexpectedItem=expectedArray[expectedIdx];
431-
letfound=false;
432-
428+
expectedIteration: for(constexpectedItemofexpectedIterator){
433429
for(letactualIdx=0;actualIdx<actualArray.length;actualIdx++){
434430
if(!usedIndices.has(actualIdx)&&isDeepStrictEqual(actualArray[actualIdx],expectedItem)){
435431
usedIndices.add(actualIdx);
436-
found=true;
437-
break;
432+
continue expectedIteration;
438433
}
439434
}
440-
441-
if(!found){
442-
returnfalse;
443-
}
435+
returnfalse;
444436
}
445437

446438
returntrue;

0 commit comments

Comments
 (0)