From e9dc6306ca97598d6a8fe9baa7c8a8ed002287b2 Mon Sep 17 00:00:00 2001 From: Brian White Date: Tue, 19 Apr 2016 19:40:28 -0400 Subject: [PATCH] url: use "empty" object for empty query strings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes things consistent with the way that the querystring module creates parsed results. PR-URL: https://github.com/nodejs/node/pull/6289 Reviewed-By: James M Snell Reviewed-By: Michaël Zasso Reviewed-By: Сковорода Никита Андреевич Reviewed-By: Minwoo Jung Reviewed-By: Colin Ihrig --- lib/url.js | 10 ++++++++-- test/parallel/test-url.js | 4 +++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/url.js b/lib/url.js index bb11144f7a3..8b9f92856d4 100644 --- a/lib/url.js +++ b/lib/url.js @@ -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; @@ -168,7 +174,7 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) { } } else if (parseQueryString) { this.search = ''; - this.query = {}; + this.query = new ParsedQueryString(); } return this; } @@ -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 && diff --git a/test/parallel/test-url.js b/test/parallel/test-url.js index 760303e98bb..1499fb87335 100644 --- a/test/parallel/test-url.js +++ b/test/parallel/test-url.js @@ -927,7 +927,7 @@ var parseTestsWithQueryString = { path: '/example', href: '/example' }, - '/example?query=value':{ + '/example?query=value': { protocol: null, slashes: null, auth: null, @@ -951,6 +951,8 @@ for (const u in parseTestsWithQueryString) { } } + assert.notStrictEqual(Object.getPrototypeOf(actual.query), Object.prototype); + assert.deepEqual(actual, expected); }