2323const {
2424 ArrayBufferIsView,
2525 ArrayBufferPrototypeGetByteLength,
26- ArrayFrom,
2726 ArrayIsArray,
2827 ArrayPrototypeIndexOf,
2928 ArrayPrototypeJoin,
@@ -395,12 +394,11 @@ function partiallyCompareMaps(actual, expected, comparedObjects) {
395394 const expectedIterator = FunctionPrototypeCall ( SafeMap . prototype [ SymbolIterator ] , expected ) ;
396395
397396 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 ) ) {
399399 return false ;
400400 }
401401
402- const actualValue = MapPrototypeGet ( actual , key ) ;
403-
404402 if ( ! compareBranch ( actualValue , expectedValue , comparedObjects ) ) {
405403 return false ;
406404 }
@@ -481,18 +479,30 @@ function partiallyCompareSets(actual, expected, comparedObjects) {
481479
482480 if ( isDeepEqual === undefined ) lazyLoadComparison ( ) ;
483481
484- const actualArray = ArrayFrom ( FunctionPrototypeCall ( SafeSet . prototype [ SymbolIterator ] , actual ) ) ;
482+ // Create a map for faster lookups and counts
483+ const actualMap = new SafeMap ( ) ;
484+ const actualIterator = FunctionPrototypeCall ( SafeSet . prototype [ SymbolIterator ] , actual ) ;
485485 const expectedIterator = FunctionPrototypeCall ( SafeSet . prototype [ SymbolIterator ] , expected ) ;
486- const usedIndices = new SafeSet ( ) ;
487486
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;
487+ for ( const actualItem of actualIterator ) {
488+ actualMap . set ( actualItem , ( actualMap . get ( actualItem ) || 0 ) + 1 ) ;
489+ }
490+
491+ const actualMapIterator = FunctionPrototypeCall ( SafeMap . prototype [ SymbolIterator ] , actualMap ) ;
492+
493+ for ( const expectedItem of expectedIterator ) {
494+ let foundMatch = false ;
495+ for ( const { 0 : actualItem , 1 : count } of actualMapIterator ) {
496+ if ( count > 0 && isDeepStrictEqual ( actualItem , expectedItem ) ) {
497+ actualMap . set ( actualItem , count - 1 ) ;
498+ foundMatch = true ;
499+ break ;
493500 }
494501 }
495- return false ;
502+
503+ if ( ! foundMatch ) {
504+ return false ;
505+ }
496506 }
497507
498508 return true ;
@@ -514,17 +524,15 @@ function partiallyCompareArrays(actual, expected, comparedObjects) {
514524 return false ;
515525 }
516526
517- if ( isDeepEqual === undefined ) lazyLoadComparison ( ) ;
527+ if ( typeof isDeepStrictEqual === ' undefined' ) lazyLoadComparison ( ) ;
518528
519529 // Create a map to count occurrences of each element in the expected array
520530 const expectedCounts = new SafeMap ( ) ;
521- const safeExpected = new SafeArrayIterator ( expected ) ;
522531
523- for ( const expectedItem of safeExpected ) {
524- // Check if the item is a zero or a -0, as these need to be handled separately
532+ for ( const expectedItem of new SafeArrayIterator ( expected ) ) {
525533 if ( expectedItem === 0 ) {
526534 const zeroKey = getZeroKey ( expectedItem ) ;
527- expectedCounts . set ( zeroKey , ( expectedCounts . get ( zeroKey ) ?. count || 0 ) + 1 ) ;
535+ expectedCounts . set ( zeroKey , ( expectedCounts . get ( zeroKey ) || 0 ) + 1 ) ;
528536 } else {
529537 let found = false ;
530538 for ( const { 0 : key , 1 : count } of expectedCounts ) {
@@ -540,10 +548,7 @@ function partiallyCompareArrays(actual, expected, comparedObjects) {
540548 }
541549 }
542550
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
551+ for ( const actualItem of new SafeArrayIterator ( actual ) ) {
547552 if ( actualItem === 0 ) {
548553 const zeroKey = getZeroKey ( actualItem ) ;
549554
@@ -554,6 +559,7 @@ function partiallyCompareArrays(actual, expected, comparedObjects) {
554559 } else {
555560 expectedCounts . set ( zeroKey , count - 1 ) ;
556561 }
562+ continue ;
557563 }
558564 } else {
559565 for ( const { 0 : expectedItem , 1 : count } of expectedCounts ) {
0 commit comments