Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

url: use "empty" object for empty query strings #6289

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ const slashedProtocol = {
};
const querystring = require('querystring');

// This constructor is used to store parsed query string values. Instantiating
// this is faster than explicitly calling `Object.create(null)` to get a
// "clean" empty object (tested with v8 v4.9).
function ParsedQueryString() {}
ParsedQueryString.prototype = Object.create(null);

function urlParse(url, parseQueryString, slashesDenoteHost) {
if (url instanceof Url) return url;

Expand Down Expand Up @@ -168,7 +174,7 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
}
} else if (parseQueryString) {
this.search = '';
this.query = {};
this.query = new ParsedQueryString();
}
return this;
}
Expand Down Expand Up @@ -358,7 +364,7 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
} else if (parseQueryString) {
// no query string, but parseQueryString still requested
this.search = '';
this.query = {};
this.query = new ParsedQueryString();
}

var firstIdx = (questionIdx !== -1 &&
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ var parseTestsWithQueryString = {
path: '/example',
href: '/example'
},
'/example?query=value':{
'/example?query=value': {
protocol: null,
slashes: null,
auth: null,
Expand All @@ -951,6 +951,8 @@ for (const u in parseTestsWithQueryString) {
}
}

assert.notStrictEqual(Object.getPrototypeOf(actual.query), Object.prototype);

assert.deepEqual(actual, expected);
}

Expand Down