From 36aeae8610d478760460cb5f14103a940c20b0d8 Mon Sep 17 00:00:00 2001 From: CGavrila Date: Tue, 28 Oct 2014 12:08:37 +0000 Subject: [PATCH 1/2] url: improve parsing speed The url.parse() function now checks whether an escapable character is in the URL before trying to escape it. PR-URL: https://github.com/joyent/node/pull/8638 Reviewed-By: Ben Noordhuis --- lib/url.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/url.js b/lib/url.js index 56b1be9328e2a0..c9d8b9df3957ac 100644 --- a/lib/url.js +++ b/lib/url.js @@ -319,11 +319,13 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) { // need to be. for (var i = 0, l = autoEscape.length; i < l; i++) { var ae = autoEscape[i]; - var esc = encodeURIComponent(ae); - if (esc === ae) { - esc = escape(ae); + if (rest.indexOf(ae) !== -1) { + var esc = encodeURIComponent(ae); + if (esc === ae) { + esc = escape(ae); + } + rest = rest.split(ae).join(esc); } - rest = rest.split(ae).join(esc); } } From b7fc957c893451b4097f3282864762235305d3a3 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 6 Dec 2014 20:38:58 +0100 Subject: [PATCH 2/2] benchmark: add url benchmarks Based on the ad-hoc benchmark from joyent/node#8638. --- benchmark/url/url.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 benchmark/url/url.js diff --git a/benchmark/url/url.js b/benchmark/url/url.js new file mode 100644 index 00000000000000..7116b94d60677c --- /dev/null +++ b/benchmark/url/url.js @@ -0,0 +1,26 @@ +var common = require('../common.js'); +var url = require('url'); + +var bench = common.createBenchmark(main, { + type: 'one two three four five'.split(' '), + n: [25e4] +}); + +function main(conf) { + var type = conf.type; + var n = conf.n | 0; + + var inputs = { + one: 'http://nodejs.org/docs/latest/api/url.html#url_url_format_urlobj', + two: 'http://blog.nodejs.org/', + three: 'https://encrypted.google.com/search?q=url&q=site:npmjs.org&hl=en', + four: 'javascript:alert("node is awesome");', + five: 'some.ran/dom/url.thing?oh=yes#whoo', + }; + var input = inputs[type] || ''; + + bench.start(); + for (var i = 0; i < n; i += 1) + url.parse(input); + bench.end(n); +}