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,39 @@ 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 ) ) ;
482
+ const actualSet = new SafeSet ( ) ;
483
+ for ( const item of FunctionPrototypeCall ( SafeSet . prototype [ SymbolIterator ] , actual ) ) {
484
+ if ( ! isPrimitive ( item ) ) {
485
+ actualSet . add ( item ) ;
486
+ }
487
+ }
488
+
485
489
const expectedIterator = FunctionPrototypeCall ( SafeSet . prototype [ SymbolIterator ] , expected ) ;
486
- const usedIndices = new SafeSet ( ) ;
487
490
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;
491
+ for ( const expectedItem of expectedIterator ) {
492
+ let foundMatch = false ;
493
+
494
+ // Check for direct inclusion first (fast path for both primitives and identical object references)
495
+ if ( actual . has ( expectedItem ) ) {
496
+ foundMatch = true ;
497
+ } else if ( ! isPrimitive ( expectedItem ) ) {
498
+ for ( const actualItem of actualSet ) {
499
+ if ( isDeepStrictEqual ( actualItem , expectedItem ) ) {
500
+ actualSet . delete ( actualItem ) ;
501
+ foundMatch = true ;
502
+ break ;
503
+ }
493
504
}
494
505
}
495
- return false ;
506
+
507
+ if ( ! foundMatch ) {
508
+ return false ;
509
+ }
496
510
}
497
511
498
512
return true ;
@@ -510,21 +524,26 @@ function getZeroKey(item) {
510
524
}
511
525
512
526
function partiallyCompareArrays ( actual , expected , comparedObjects ) {
527
+ if ( actual === expected ) return true ;
528
+
513
529
if ( expected . length > actual . length ) {
514
530
return false ;
515
531
}
516
532
533
+ if ( expected . length === 0 ) {
534
+ return true ;
535
+ }
536
+
517
537
if ( isDeepEqual === undefined ) lazyLoadComparison ( ) ;
518
538
519
539
// Create a map to count occurrences of each element in the expected array
520
540
const expectedCounts = new SafeMap ( ) ;
521
- const safeExpected = new SafeArrayIterator ( expected ) ;
522
541
523
- for ( const expectedItem of safeExpected ) {
524
- // Check if the item is a zero or a -0, as these need to be handled separately
542
+ const expectedIterator = new SafeArrayIterator ( expected ) ;
543
+ for ( const expectedItem of expectedIterator ) {
525
544
if ( expectedItem === 0 ) {
526
545
const zeroKey = getZeroKey ( expectedItem ) ;
527
- expectedCounts . set ( zeroKey , ( expectedCounts . get ( zeroKey ) ?. count || 0 ) + 1 ) ;
546
+ expectedCounts . set ( zeroKey , ( expectedCounts . get ( zeroKey ) ?? 0 ) + 1 ) ;
528
547
} else {
529
548
let found = false ;
530
549
for ( const { 0 : key , 1 : count } of expectedCounts ) {
@@ -540,10 +559,8 @@ function partiallyCompareArrays(actual, expected, comparedObjects) {
540
559
}
541
560
}
542
561
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
562
+ const actualIterator = new SafeArrayIterator ( actual ) ;
563
+ for ( const actualItem of actualIterator ) {
547
564
if ( actualItem === 0 ) {
548
565
const zeroKey = getZeroKey ( actualItem ) ;
549
566
@@ -567,6 +584,10 @@ function partiallyCompareArrays(actual, expected, comparedObjects) {
567
584
}
568
585
}
569
586
}
587
+
588
+ if ( expectedCounts . size === 0 ) {
589
+ return true ;
590
+ }
570
591
}
571
592
572
593
return expectedCounts . size === 0 ;
@@ -723,6 +744,10 @@ function compareExceptionKey(actual, expected, key, message, keys, fn) {
723
744
}
724
745
}
725
746
747
+ function isPrimitive ( value ) {
748
+ return typeof value !== 'object' || value === null ;
749
+ }
750
+
726
751
function expectedException ( actual , expected , message , fn ) {
727
752
let generatedMessage = false ;
728
753
let throwError = false ;
@@ -741,7 +766,7 @@ function expectedException(actual, expected, message, fn) {
741
766
}
742
767
throwError = true ;
743
768
// Handle primitives properly.
744
- } else if ( typeof actual !== 'object' || actual === null ) {
769
+ } else if ( isPrimitive ( actual ) ) {
745
770
const err = new AssertionError ( {
746
771
actual,
747
772
expected,
0 commit comments