23
23
const {
24
24
ArrayBufferIsView,
25
25
ArrayBufferPrototypeGetByteLength,
26
- ArrayFrom,
27
26
ArrayIsArray,
28
27
ArrayPrototypeIndexOf,
29
28
ArrayPrototypeJoin,
@@ -395,12 +394,11 @@ function partiallyCompareMaps(actual, expected, comparedObjects) {
395
394
const expectedIterator = FunctionPrototypeCall ( SafeMap . prototype [ SymbolIterator ] , expected ) ;
396
395
397
396
for ( const { 0 : key , 1 : expectedValue } of expectedIterator ) {
398
- if ( ! MapPrototypeHas ( actual , key ) ) {
397
+ const actualValue = MapPrototypeGet ( actual , key ) ;
398
+ if ( actualValue === undefined && ! MapPrototypeHas ( actual , key ) ) {
399
399
return false ;
400
400
}
401
401
402
- const actualValue = MapPrototypeGet ( actual , key ) ;
403
-
404
402
if ( ! compareBranch ( actualValue , expectedValue , comparedObjects ) ) {
405
403
return false ;
406
404
}
@@ -476,23 +474,44 @@ function partiallyCompareArrayBuffersOrViews(actual, expected) {
476
474
477
475
function partiallyCompareSets ( actual , expected , comparedObjects ) {
478
476
if ( SetPrototypeGetSize ( expected ) > SetPrototypeGetSize ( actual ) ) {
479
- return false ; // `expected` can't be a subset if it has more elements
477
+ return false ;
480
478
}
481
479
482
480
if ( isDeepEqual === undefined ) lazyLoadComparison ( ) ;
483
481
484
- const actualArray = ArrayFrom ( FunctionPrototypeCall ( SafeSet . prototype [ SymbolIterator ] , actual ) ) ;
485
482
const expectedIterator = FunctionPrototypeCall ( SafeSet . prototype [ SymbolIterator ] , expected ) ;
486
- const usedIndices = new SafeSet ( ) ;
483
+ let actualSet ;
484
+
485
+ for ( const expectedItem of expectedIterator ) {
486
+ let foundMatch = false ;
487
+
488
+ // Check for direct inclusion first (fast path for both primitives and identical object references)
489
+ if ( actual . has ( expectedItem ) ) {
490
+ foundMatch = true ;
491
+ } else if ( ! isPrimitive ( expectedItem ) ) {
492
+ // Only create actualSet if we need it, and only add objects to it
493
+ if ( actualSet === undefined ) {
494
+ actualSet = new SafeSet ( ) ;
495
+
496
+ for ( const item of FunctionPrototypeCall ( SafeSet . prototype [ SymbolIterator ] , actual ) ) {
497
+ if ( ! isPrimitive ( item ) ) {
498
+ actualSet . add ( item ) ;
499
+ }
500
+ }
501
+ }
487
502
488
- expectedIteration: for ( const expectedItem of expectedIterator ) {
489
- for ( let actualIdx = 0 ; actualIdx < actualArray . length ; actualIdx ++ ) {
490
- if ( ! usedIndices . has ( actualIdx ) && isDeepStrictEqual ( actualArray [ actualIdx ] , expectedItem ) ) {
491
- usedIndices . add ( actualIdx ) ;
492
- continue expectedIteration;
503
+ for ( const actualItem of actualSet ) {
504
+ if ( isDeepStrictEqual ( actualItem , expectedItem ) ) {
505
+ actualSet . delete ( actualItem ) ;
506
+ foundMatch = true ;
507
+ break ;
508
+ }
493
509
}
494
510
}
495
- return false ;
511
+
512
+ if ( ! foundMatch ) {
513
+ return false ;
514
+ }
496
515
}
497
516
498
517
return true ;
@@ -510,21 +529,26 @@ function getZeroKey(item) {
510
529
}
511
530
512
531
function partiallyCompareArrays ( actual , expected , comparedObjects ) {
532
+ if ( actual === expected ) return true ;
533
+
513
534
if ( expected . length > actual . length ) {
514
535
return false ;
515
536
}
516
537
538
+ if ( expected . length === 0 ) {
539
+ return true ;
540
+ }
541
+
517
542
if ( isDeepEqual === undefined ) lazyLoadComparison ( ) ;
518
543
519
544
// Create a map to count occurrences of each element in the expected array
520
545
const expectedCounts = new SafeMap ( ) ;
521
- const safeExpected = new SafeArrayIterator ( expected ) ;
522
546
523
- for ( const expectedItem of safeExpected ) {
524
- // Check if the item is a zero or a -0, as these need to be handled separately
547
+ const expectedIterator = new SafeArrayIterator ( expected ) ;
548
+ for ( const expectedItem of expectedIterator ) {
525
549
if ( expectedItem === 0 ) {
526
550
const zeroKey = getZeroKey ( expectedItem ) ;
527
- expectedCounts . set ( zeroKey , ( expectedCounts . get ( zeroKey ) ?. count || 0 ) + 1 ) ;
551
+ expectedCounts . set ( zeroKey , ( expectedCounts . get ( zeroKey ) ?? 0 ) + 1 ) ;
528
552
} else {
529
553
let found = false ;
530
554
for ( const { 0 : key , 1 : count } of expectedCounts ) {
@@ -540,10 +564,8 @@ function partiallyCompareArrays(actual, expected, comparedObjects) {
540
564
}
541
565
}
542
566
543
- const safeActual = new SafeArrayIterator ( actual ) ;
544
-
545
- for ( const actualItem of safeActual ) {
546
- // Check if the item is a zero or a -0, as these need to be handled separately
567
+ const actualIterator = new SafeArrayIterator ( actual ) ;
568
+ for ( const actualItem of actualIterator ) {
547
569
if ( actualItem === 0 ) {
548
570
const zeroKey = getZeroKey ( actualItem ) ;
549
571
@@ -567,6 +589,10 @@ function partiallyCompareArrays(actual, expected, comparedObjects) {
567
589
}
568
590
}
569
591
}
592
+
593
+ if ( expectedCounts . size === 0 ) {
594
+ return true ;
595
+ }
570
596
}
571
597
572
598
return expectedCounts . size === 0 ;
@@ -723,6 +749,10 @@ function compareExceptionKey(actual, expected, key, message, keys, fn) {
723
749
}
724
750
}
725
751
752
+ function isPrimitive ( value ) {
753
+ return typeof value !== 'object' || value === null ;
754
+ }
755
+
726
756
function expectedException ( actual , expected , message , fn ) {
727
757
let generatedMessage = false ;
728
758
let throwError = false ;
@@ -741,7 +771,7 @@ function expectedException(actual, expected, message, fn) {
741
771
}
742
772
throwError = true ;
743
773
// Handle primitives properly.
744
- } else if ( typeof actual !== 'object' || actual === null ) {
774
+ } else if ( isPrimitive ( actual ) ) {
745
775
const err = new AssertionError ( {
746
776
actual,
747
777
expected,
0 commit comments