@@ -22,6 +22,7 @@ const {
22
22
ReflectOwnKeys,
23
23
RegExpPrototypeSymbolReplace,
24
24
SafeMap,
25
+ SafeWeakMap,
25
26
StringPrototypeCharAt,
26
27
StringPrototypeCharCodeAt,
27
28
StringPrototypeCodePointAt,
@@ -99,6 +100,12 @@ const FORWARD_SLASH = /\//g;
99
100
const context = Symbol ( 'context' ) ;
100
101
const searchParams = Symbol ( 'query' ) ;
101
102
103
+ /**
104
+ * We cannot use private fields without a breaking change, so a WeakMap is the alternative.
105
+ * @type {WeakMap<URL,URLSearchParams> }
106
+ */
107
+ const internalSearchParams = new SafeWeakMap ( ) ;
108
+
102
109
const updateActions = {
103
110
kProtocol : 0 ,
104
111
kHost : 1 ,
@@ -179,7 +186,7 @@ class URLContext {
179
186
}
180
187
181
188
function isURLSearchParams ( self ) {
182
- return self && self [ searchParams ] && ! self [ searchParams ] [ searchParams ] ;
189
+ return self ?. [ searchParams ] ;
183
190
}
184
191
185
192
class URLSearchParams {
@@ -672,11 +679,12 @@ class URL {
672
679
ctx . hash_start = hash_start ;
673
680
ctx . scheme_type = scheme_type ;
674
681
675
- if ( this [ searchParams ] ) {
682
+ const alreadyInstantiatedSearchParams = internalSearchParams . get ( this ) ;
683
+ if ( alreadyInstantiatedSearchParams ) {
676
684
if ( ctx . hasSearch ) {
677
- this [ searchParams ] [ searchParams ] = parseParams ( this . search ) ;
685
+ alreadyInstantiatedSearchParams [ searchParams ] = parseParams ( this . search ) ;
678
686
} else {
679
- this [ searchParams ] [ searchParams ] = [ ] ;
687
+ alreadyInstantiatedSearchParams [ searchParams ] = [ ] ;
680
688
}
681
689
}
682
690
}
@@ -898,11 +906,15 @@ class URL {
898
906
get searchParams ( ) {
899
907
if ( ! isURL ( this ) )
900
908
throw new ERR_INVALID_THIS ( 'URL' ) ;
901
- if ( this [ searchParams ] == null ) {
902
- this [ searchParams ] = new URLSearchParams ( this . search ) ;
903
- this [ searchParams ] [ context ] = this ;
904
- }
905
- return this [ searchParams ] ;
909
+
910
+ const cachedValue = internalSearchParams . get ( this ) ;
911
+ if ( cachedValue != null )
912
+ return cachedValue ;
913
+
914
+ const value = new URLSearchParams ( this . search ) ;
915
+ value [ context ] = this ;
916
+ internalSearchParams . set ( this , value ) ;
917
+ return value ;
906
918
}
907
919
908
920
get hash ( ) {
0 commit comments