diff --git a/lib/internal/url.js b/lib/internal/url.js index 7a67fe2f15e..ffd8f10edae 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -27,7 +27,9 @@ const { CHAR_LOWERCASE_A, CHAR_LOWERCASE_Z, } = require('internal/constants'); -const querystring = require('querystring'); + +// Lazy loaded for startup performance. +let querystring; const { platform } = process; const isWindows = platform === 'win32'; @@ -771,8 +773,10 @@ function parseParams(qs) { } else if (encodeCheck > 0) { // eslint-disable-next-line no-extra-boolean-cast if (!!isHexTable[code]) { - if (++encodeCheck === 3) + if (++encodeCheck === 3) { + querystring = require('querystring'); encoded = true; + } } else { encodeCheck = 0; } diff --git a/lib/url.js b/lib/url.js index e4326e80b5d..c517ba7ab3d 100644 --- a/lib/url.js +++ b/lib/url.js @@ -94,7 +94,6 @@ const slashedProtocol = { 'file': true, 'file:': true }; -const querystring = require('querystring'); const { CHAR_SPACE, CHAR_TAB, @@ -133,6 +132,9 @@ const { CHAR_AT, } = require('internal/constants'); +// Lazy loaded for startup performance. +let querystring; + function urlParse(url, parseQueryString, slashesDenoteHost) { if (url instanceof Url) return url; @@ -233,6 +235,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) { if (simplePath[2]) { this.search = simplePath[2]; if (parseQueryString) { + if (querystring === undefined) querystring = require('querystring'); this.query = querystring.parse(this.search.slice(1)); } else { this.query = this.search.slice(1); @@ -422,6 +425,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) { this.query = rest.slice(questionIdx + 1, hashIdx); } if (parseQueryString) { + if (querystring === undefined) querystring = require('querystring'); this.query = querystring.parse(this.query); } } else if (parseQueryString) { @@ -584,8 +588,10 @@ Url.prototype.format = function format() { } } - if (this.query !== null && typeof this.query === 'object') + if (this.query !== null && typeof this.query === 'object') { + if (querystring === undefined) querystring = require('querystring'); query = querystring.stringify(this.query); + } var search = this.search || (query && ('?' + query)) || '';