@@ -494,26 +494,37 @@ class URLSearchParams {
494494
495495 const list = this . #searchParams;
496496 name = StringPrototypeToWellFormed ( `${ name } ` ) ;
497+ const { length } = list ;
498+ let write = 0 ;
497499
498500 if ( value !== undefined ) {
499501 value = StringPrototypeToWellFormed ( `${ value } ` ) ;
500- for ( let i = 0 ; i < list . length ; ) {
502+ for ( let i = 0 ; i < length ; i += 2 ) {
501503 if ( list [ i ] === name && list [ i + 1 ] === value ) {
502- list . splice ( i , 2 ) ;
503- } else {
504- i += 2 ;
504+ continue ;
505+ }
506+ if ( write !== i ) {
507+ list [ write ] = list [ i ] ;
508+ list [ write + 1 ] = list [ i + 1 ] ;
505509 }
510+ write += 2 ;
506511 }
507512 } else {
508- for ( let i = 0 ; i < list . length ; ) {
513+ for ( let i = 0 ; i < length ; i += 2 ) {
509514 if ( list [ i ] === name ) {
510- list . splice ( i , 2 ) ;
511- } else {
512- i += 2 ;
515+ continue ;
516+ }
517+ if ( write !== i ) {
518+ list [ write ] = list [ i ] ;
519+ list [ write + 1 ] = list [ i + 1 ] ;
513520 }
521+ write += 2 ;
514522 }
515523 }
516524
525+ if ( write !== length )
526+ list . length = write ;
527+
517528 if ( this . #context) {
518529 setURLSearchParamsModified ( this . #context) ;
519530 }
@@ -593,24 +604,34 @@ class URLSearchParams {
593604 const list = this . #searchParams;
594605 name = StringPrototypeToWellFormed ( `${ name } ` ) ;
595606 value = StringPrototypeToWellFormed ( `${ value } ` ) ;
607+ const { length } = list ;
596608
597609 // If there are any name-value pairs whose name is `name`, in `list`, set
598610 // the value of the first such name-value pair to `value` and remove the
599611 // others.
600612 let found = false ;
601- for ( let i = 0 ; i < list . length ; ) {
613+ let write = 0 ;
614+ for ( let i = 0 ; i < length ; i += 2 ) {
602615 const cur = list [ i ] ;
616+ let keep = true ;
603617 if ( cur === name ) {
604618 if ( ! found ) {
605- list [ i + 1 ] = value ;
619+ list [ write ] = cur ;
620+ list [ write + 1 ] = value ;
606621 found = true ;
607- i += 2 ;
608622 } else {
609- list . splice ( i , 2 ) ;
623+ keep = false ;
610624 }
611- } else {
612- i += 2 ;
625+ } else if ( write !== i ) {
626+ list [ write ] = cur ;
627+ list [ write + 1 ] = list [ i + 1 ] ;
613628 }
629+ if ( keep )
630+ write += 2 ;
631+ }
632+
633+ if ( found && write !== length ) {
634+ list . length = write ;
614635 }
615636
616637 // Otherwise, append a new name-value pair whose name is `name` and value
0 commit comments