Skip to content

Commit 59737b2

Browse files
mscdexjasnell
authored andcommitted
url: use "empty" object for empty query strings
This makes things consistent with the way that the querystring module creates parsed results. PR-URL: #6289 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <mic.besace@gmail.com> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: Minwoo Jung <jmwsoft@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 809ed52 commit 59737b2

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/url.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ const slashedProtocol = {
6060
};
6161
const querystring = require('querystring');
6262

63+
// This constructor is used to store parsed query string values. Instantiating
64+
// this is faster than explicitly calling `Object.create(null)` to get a
65+
// "clean" empty object (tested with v8 v4.9).
66+
function ParsedQueryString() {}
67+
ParsedQueryString.prototype = Object.create(null);
68+
6369
function urlParse(url, parseQueryString, slashesDenoteHost) {
6470
if (url instanceof Url) return url;
6571

@@ -168,7 +174,7 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
168174
}
169175
} else if (parseQueryString) {
170176
this.search = '';
171-
this.query = {};
177+
this.query = new ParsedQueryString();
172178
}
173179
return this;
174180
}
@@ -358,7 +364,7 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
358364
} else if (parseQueryString) {
359365
// no query string, but parseQueryString still requested
360366
this.search = '';
361-
this.query = {};
367+
this.query = new ParsedQueryString();
362368
}
363369

364370
var firstIdx = (questionIdx !== -1 &&

test/parallel/test-url.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ var parseTestsWithQueryString = {
927927
path: '/example',
928928
href: '/example'
929929
},
930-
'/example?query=value':{
930+
'/example?query=value': {
931931
protocol: null,
932932
slashes: null,
933933
auth: null,
@@ -951,6 +951,8 @@ for (const u in parseTestsWithQueryString) {
951951
}
952952
}
953953

954+
assert.notStrictEqual(Object.getPrototypeOf(actual.query), Object.prototype);
955+
954956
assert.deepEqual(actual, expected);
955957
}
956958

0 commit comments

Comments
 (0)