@@ -279,8 +279,16 @@ function copyAndInsertPair(node, generation, bit, idx, key, val) {
279279
280280function copyAndRemovePair ( node , generation , bit , idx ) {
281281 node = copyNode ( node , generation ) ;
282+
283+ const data = node . data ;
284+ const length = data . length ;
285+ for ( let w = idx , r = idx + 2 ; r < length ; ++ r , ++ w ) {
286+ data [ w ] = data [ r ] ;
287+ }
288+ data . pop ( ) ;
289+ data . pop ( ) ;
290+
282291 node . datamap ^= bit ;
283- node . data . splice ( idx , 2 ) ;
284292 return node ;
285293}
286294
@@ -563,20 +571,24 @@ function doRemove(transient, node, key, hash, shift) {
563571 return copyAndSet ( node , generation , nodeidx , newChild ) ;
564572 }
565573
566- // when writing, it looks like since we delete first it's not too bad.
567- node = copyNode ( node , generation ) ;
568574 // this node only has a single data (k/v-pair) child.
569575 // to restore the CHAMP invariant, we "pull" that pair up into ourselves.
570576 // this ensures that every tree stays in its single optimal representation,
571577 // and allows dicts to be structurally compared.
572- node . datamap |= bit ;
573- node . nodemap ^= bit ;
574- // NOTE: the order here is important to avoid mutation bugs!
575- // Remove the old child node, and insert the data pair into ourselves.
576- node . data . splice ( nodeidx , 1 ) ;
577- node . data . splice ( dataidx , 0 , newChild . data [ 0 ] , newChild . data [ 1 ] ) ;
578+ const length = data . length ;
579+ const newData = new Array ( length + 1 ) ;
578580
579- return node ;
581+ let readIndex = 0 ;
582+ let writeIndex = 0 ;
583+
584+ while ( readIndex < dataidx ) newData [ writeIndex ++ ] = data [ readIndex ++ ] ;
585+ newData [ writeIndex ++ ] = newChild . data [ 0 ] ;
586+ newData [ writeIndex ++ ] = newChild . data [ 1 ] ;
587+ while ( readIndex < nodeidx ) newData [ writeIndex ++ ] = data [ readIndex ++ ] ;
588+ readIndex ++ ;
589+ while ( readIndex < length ) newData [ writeIndex ++ ] = data [ readIndex ++ ] ;
590+
591+ return makeNode ( generation , node . datamap | bit , node . nodemap ^ bit , newData ) ;
580592 }
581593
582594 // 3. Data Node
0 commit comments