@@ -224,7 +224,7 @@ class URLSearchParams {
224224} else {
225225// USVString
226226init = toUSVString ( init ) ;
227- initSearchParams ( this , init ) ;
227+ this [ searchParams ] = init ? parseParams ( init ) : [ ] ;
228228}
229229
230230// "associated url object"
@@ -534,7 +534,7 @@ ObjectDefineProperties(URLSearchParams.prototype, {
534534} ,
535535} ) ;
536536
537- function isURLThis ( self ) {
537+ function isURL ( self ) {
538538return self != null && ObjectPrototypeHasOwnProperty ( self , context ) ;
539539}
540540
@@ -602,160 +602,161 @@ class URL {
602602ctx . password = password ;
603603ctx . port = port ;
604604ctx . hash = hash ;
605- if ( ! this [ searchParams ] ) { // Invoked from URL constructor
606- this [ searchParams ] = new URLSearchParams ( ) ;
607- this [ searchParams ] [ context ] = this ;
605+ if ( this [ searchParams ] ) {
606+ this [ searchParams ] [ searchParams ] = parseParams ( search ) ;
608607}
609- initSearchParams ( this [ searchParams ] , ctx . search ) ;
610608} ;
611609
612610toString ( ) {
613- if ( ! isURLThis ( this ) )
611+ if ( ! isURL ( this ) )
614612throw new ERR_INVALID_THIS ( 'URL' ) ;
615613return this [ context ] . href ;
616614}
617615
618616get href ( ) {
619- if ( ! isURLThis ( this ) )
617+ if ( ! isURL ( this ) )
620618throw new ERR_INVALID_THIS ( 'URL' ) ;
621619return this [ context ] . href ;
622620}
623621
624622set href ( value ) {
625- if ( ! isURLThis ( this ) )
623+ if ( ! isURL ( this ) )
626624throw new ERR_INVALID_THIS ( 'URL' ) ;
627625const valid = updateUrl ( this [ context ] . href , updateActions . kHref , `${ value } ` , this . #onParseComplete) ;
628626if ( ! valid ) { throw ERR_INVALID_URL ( `${ value } ` ) ; }
629627}
630628
631629// readonly
632630get origin ( ) {
633- if ( ! isURLThis ( this ) )
631+ if ( ! isURL ( this ) )
634632throw new ERR_INVALID_THIS ( 'URL' ) ;
635633return this [ context ] . origin ;
636634}
637635
638636get protocol ( ) {
639- if ( ! isURLThis ( this ) )
637+ if ( ! isURL ( this ) )
640638throw new ERR_INVALID_THIS ( 'URL' ) ;
641639return this [ context ] . protocol ;
642640}
643641
644642set protocol ( value ) {
645- if ( ! isURLThis ( this ) )
643+ if ( ! isURL ( this ) )
646644throw new ERR_INVALID_THIS ( 'URL' ) ;
647645updateUrl ( this [ context ] . href , updateActions . kProtocol , `${ value } ` , this . #onParseComplete) ;
648646}
649647
650648get username ( ) {
651- if ( ! isURLThis ( this ) )
649+ if ( ! isURL ( this ) )
652650throw new ERR_INVALID_THIS ( 'URL' ) ;
653651return this [ context ] . username ;
654652}
655653
656654set username ( value ) {
657- if ( ! isURLThis ( this ) )
655+ if ( ! isURL ( this ) )
658656throw new ERR_INVALID_THIS ( 'URL' ) ;
659657updateUrl ( this [ context ] . href , updateActions . kUsername , `${ value } ` , this . #onParseComplete) ;
660658}
661659
662660get password ( ) {
663- if ( ! isURLThis ( this ) )
661+ if ( ! isURL ( this ) )
664662throw new ERR_INVALID_THIS ( 'URL' ) ;
665663return this [ context ] . password ;
666664}
667665
668666set password ( value ) {
669- if ( ! isURLThis ( this ) )
667+ if ( ! isURL ( this ) )
670668throw new ERR_INVALID_THIS ( 'URL' ) ;
671669updateUrl ( this [ context ] . href , updateActions . kPassword , `${ value } ` , this . #onParseComplete) ;
672670}
673671
674672get host ( ) {
675- if ( ! isURLThis ( this ) )
673+ if ( ! isURL ( this ) )
676674throw new ERR_INVALID_THIS ( 'URL' ) ;
677675const port = this [ context ] . port ;
678676const suffix = port . length > 0 ? `:${ port } ` : '' ;
679677return this [ context ] . hostname + suffix ;
680678}
681679
682680set host ( value ) {
683- if ( ! isURLThis ( this ) )
681+ if ( ! isURL ( this ) )
684682throw new ERR_INVALID_THIS ( 'URL' ) ;
685683updateUrl ( this [ context ] . href , updateActions . kHost , `${ value } ` , this . #onParseComplete) ;
686684}
687685
688686get hostname ( ) {
689- if ( ! isURLThis ( this ) )
687+ if ( ! isURL ( this ) )
690688throw new ERR_INVALID_THIS ( 'URL' ) ;
691689return this [ context ] . hostname ;
692690}
693691
694692set hostname ( value ) {
695- if ( ! isURLThis ( this ) )
693+ if ( ! isURL ( this ) )
696694throw new ERR_INVALID_THIS ( 'URL' ) ;
697695updateUrl ( this [ context ] . href , updateActions . kHostname , `${ value } ` , this . #onParseComplete) ;
698696}
699697
700698get port ( ) {
701- if ( ! isURLThis ( this ) )
699+ if ( ! isURL ( this ) )
702700throw new ERR_INVALID_THIS ( 'URL' ) ;
703701return this [ context ] . port ;
704702}
705703
706704set port ( value ) {
707- if ( ! isURLThis ( this ) )
705+ if ( ! isURL ( this ) )
708706throw new ERR_INVALID_THIS ( 'URL' ) ;
709707updateUrl ( this [ context ] . href , updateActions . kPort , `${ value } ` , this . #onParseComplete) ;
710708}
711709
712710get pathname ( ) {
713- if ( ! isURLThis ( this ) )
711+ if ( ! isURL ( this ) )
714712throw new ERR_INVALID_THIS ( 'URL' ) ;
715713return this [ context ] . pathname ;
716714}
717715
718716set pathname ( value ) {
719- if ( ! isURLThis ( this ) )
717+ if ( ! isURL ( this ) )
720718throw new ERR_INVALID_THIS ( 'URL' ) ;
721719updateUrl ( this [ context ] . href , updateActions . kPathname , `${ value } ` , this . #onParseComplete) ;
722720}
723721
724722get search ( ) {
725- if ( ! isURLThis ( this ) )
723+ if ( ! isURL ( this ) )
726724throw new ERR_INVALID_THIS ( 'URL' ) ;
727725return this [ context ] . search ;
728726}
729727
730- set search ( search ) {
731- if ( ! isURLThis ( this ) )
728+ set search ( value ) {
729+ if ( ! isURL ( this ) )
732730throw new ERR_INVALID_THIS ( 'URL' ) ;
733- search = toUSVString ( search ) ;
734- updateUrl ( this [ context ] . href , updateActions . kSearch , search , this . #onParseComplete) ;
735- initSearchParams ( this [ searchParams ] , this [ context ] . search ) ;
731+ updateUrl ( this [ context ] . href , updateActions . kSearch , toUSVString ( value ) , this . #onParseComplete) ;
736732}
737733
738734// readonly
739735get searchParams ( ) {
740- if ( ! isURLThis ( this ) )
736+ if ( ! isURL ( this ) )
741737throw new ERR_INVALID_THIS ( 'URL' ) ;
738+ // Create URLSearchParams on demand to greatly improve the URL performance.
739+ if ( this [ searchParams ] == null ) {
740+ this [ searchParams ] = new URLSearchParams ( this [ context ] . search ) ;
741+ this [ searchParams ] [ context ] = this ;
742+ }
742743return this [ searchParams ] ;
743744}
744745
745746get hash ( ) {
746- if ( ! isURLThis ( this ) )
747+ if ( ! isURL ( this ) )
747748throw new ERR_INVALID_THIS ( 'URL' ) ;
748749return this [ context ] . hash ;
749750}
750751
751752set hash ( value ) {
752- if ( ! isURLThis ( this ) )
753+ if ( ! isURL ( this ) )
753754throw new ERR_INVALID_THIS ( 'URL' ) ;
754755updateUrl ( this [ context ] . href , updateActions . kHash , `${ value } ` , this . #onParseComplete) ;
755756}
756757
757758toJSON ( ) {
758- if ( ! isURLThis ( this ) )
759+ if ( ! isURL ( this ) )
759760throw new ERR_INVALID_THIS ( 'URL' ) ;
760761return this [ context ] . href ;
761762}
@@ -813,14 +814,6 @@ ObjectDefineProperties(URL, {
813814revokeObjectURL : kEnumerableProperty ,
814815} ) ;
815816
816- function initSearchParams ( url , init ) {
817- if ( ! init ) {
818- url [ searchParams ] = [ ] ;
819- return ;
820- }
821- url [ searchParams ] = parseParams ( init ) ;
822- }
823-
824817// application/x-www-form-urlencoded parser
825818// Ref: https://url.spec.whatwg.org/#concept-urlencoded-parser
826819function parseParams ( qs ) {
@@ -1139,8 +1132,7 @@ function domainToUnicode(domain) {
11391132function urlToHttpOptions ( url ) {
11401133const options = {
11411134protocol : url . protocol ,
1142- hostname : typeof url . hostname === 'string' &&
1143- StringPrototypeStartsWith ( url . hostname , '[' ) ?
1135+ hostname : url . hostname && StringPrototypeStartsWith ( url . hostname , '[' ) ?
11441136StringPrototypeSlice ( url . hostname , 1 , - 1 ) :
11451137url . hostname ,
11461138hash : url . hash ,
@@ -1311,6 +1303,6 @@ module.exports = {
13111303 domainToASCII,
13121304 domainToUnicode,
13131305 urlToHttpOptions,
1314- searchParamsSymbol : searchParams ,
13151306 encodeStr,
1307+ isURL,
13161308} ;
0 commit comments