From 54fbd5d2d60c5eb38870f5acfdca0950cc0aad68 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 9 Feb 2017 23:41:14 +0800 Subject: [PATCH 1/3] benchmark: move url data to fixtures --- benchmark/fixtures/url-inputs.js | 30 +++++++++++++++++++ .../url/legacy-vs-whatwg-url-get-prop.js | 14 +-------- benchmark/url/legacy-vs-whatwg-url-parse.js | 14 +-------- ...legacy-vs-whatwg-url-searchparams-parse.js | 13 +------- ...cy-vs-whatwg-url-searchparams-serialize.js | 13 +------- .../url/legacy-vs-whatwg-url-serialize.js | 14 +-------- 6 files changed, 35 insertions(+), 63 deletions(-) create mode 100644 benchmark/fixtures/url-inputs.js diff --git a/benchmark/fixtures/url-inputs.js b/benchmark/fixtures/url-inputs.js new file mode 100644 index 00000000000000..7b1983f6faa590 --- /dev/null +++ b/benchmark/fixtures/url-inputs.js @@ -0,0 +1,30 @@ +'use strict'; + +exports.urls = { + long: 'http://nodejs.org:89/docs/latest/api/foo/bar/qua/13949281/0f28b/' + + '/5d49/b3020/url.html#test?payload1=true&payload2=false&test=1' + + '&benchmark=3&foo=38.38.011.293&bar=1234834910480&test=19299&3992&' + + 'key=f5c65e1e98fe07e648249ad41e1cfdb0', + short: 'https://nodejs.org/en/blog/', + idn: 'http://你好你好.在线', + auth: 'https://user:pass@example.com/path?search=1', + file: 'file:///foo/bar/test/node.js', + ws: 'ws://localhost:9229/f46db715-70df-43ad-a359-7f9949f39868', + javascript: 'javascript:alert("node is awesome");', + percent: 'https://%E4%BD%A0/foo', + dot: 'https://example.org/./a/../b/./c' +}; + +exports.searchParams = { + noencode: 'foo=bar&baz=quux&xyzzy=thud', + multicharsep: 'foo=bar&&&&&&&&&&baz=quux&&&&&&&&&&xyzzy=thud', + encodefake: 'foo=%©ar&baz=%A©uux&xyzzy=%©ud', + encodemany: '%66%6F%6F=bar&%62%61%7A=quux&xyzzy=%74h%75d', + encodelast: 'foo=bar&baz=quux&xyzzy=thu%64', + multivalue: 'foo=bar&foo=baz&foo=quux&quuy=quuz', + multivaluemany: 'foo=bar&foo=baz&foo=quux&quuy=quuz&foo=abc&foo=def&' + + 'foo=ghi&foo=jkl&foo=mno&foo=pqr&foo=stu&foo=vwxyz', + manypairs: 'a&b&c&d&e&f&g&h&i&j&k&l&m&n&o&p&q&r&s&t&u&v&w&x&y&z', + manyblankpairs: '&&&&&&&&&&&&&&&&&&&&&&&&', + altspaces: 'foo+bar=baz+quux&xyzzy+thud=quuy+quuz&abc=def+ghi' +}; diff --git a/benchmark/url/legacy-vs-whatwg-url-get-prop.js b/benchmark/url/legacy-vs-whatwg-url-get-prop.js index f703b75b16f6f7..ffc8b4995df3de 100644 --- a/benchmark/url/legacy-vs-whatwg-url-get-prop.js +++ b/benchmark/url/legacy-vs-whatwg-url-get-prop.js @@ -3,19 +3,7 @@ const common = require('../common.js'); const url = require('url'); const URL = url.URL; const assert = require('assert'); - -const inputs = { - long: 'http://nodejs.org:89/docs/latest/api/url.html#test?' + - 'payload1=true&payload2=false&test=1&benchmark=3&' + - 'foo=38.38.011.293&bar=1234834910480&test=19299&3992&' + - 'key=f5c65e1e98fe07e648249ad41e1cfdb0', - short: 'https://nodejs.org/en/blog/', - idn: 'http://你好你好', - auth: 'https://user:pass@example.com/path?search=1', - special: 'file:///foo/bar/test/node.js', - percent: 'https://%E4%BD%A0/foo', - dot: 'https://example.org/./a/../b/./c' -}; +const inputs = require('../fixtures/url-inputs.js').urls; const bench = common.createBenchmark(main, { type: Object.keys(inputs), diff --git a/benchmark/url/legacy-vs-whatwg-url-parse.js b/benchmark/url/legacy-vs-whatwg-url-parse.js index 64533e67e4a46a..ec386b7b85597d 100644 --- a/benchmark/url/legacy-vs-whatwg-url-parse.js +++ b/benchmark/url/legacy-vs-whatwg-url-parse.js @@ -3,19 +3,7 @@ const common = require('../common.js'); const url = require('url'); const URL = url.URL; const assert = require('assert'); - -const inputs = { - long: 'http://nodejs.org:89/docs/latest/api/url.html#test?' + - 'payload1=true&payload2=false&test=1&benchmark=3&' + - 'foo=38.38.011.293&bar=1234834910480&test=19299&3992&' + - 'key=f5c65e1e98fe07e648249ad41e1cfdb0', - short: 'https://nodejs.org/en/blog/', - idn: 'http://你好你好', - auth: 'https://user:pass@example.com/path?search=1', - special: 'file:///foo/bar/test/node.js', - percent: 'https://%E4%BD%A0/foo', - dot: 'https://example.org/./a/../b/./c' -}; +const inputs = require('../fixtures/url-inputs.js').urls; const bench = common.createBenchmark(main, { type: Object.keys(inputs), diff --git a/benchmark/url/legacy-vs-whatwg-url-searchparams-parse.js b/benchmark/url/legacy-vs-whatwg-url-searchparams-parse.js index e76f2dd837bb97..86714df6c196a7 100644 --- a/benchmark/url/legacy-vs-whatwg-url-searchparams-parse.js +++ b/benchmark/url/legacy-vs-whatwg-url-searchparams-parse.js @@ -2,18 +2,7 @@ const common = require('../common.js'); const { URLSearchParams } = require('url'); const querystring = require('querystring'); - -const inputs = { - noencode: 'foo=bar&baz=quux&xyzzy=thud', - encodemany: '%66%6F%6F=bar&%62%61%7A=quux&xyzzy=%74h%75d', - encodefake: 'foo=%©ar&baz=%A©uux&xyzzy=%©ud', - encodelast: 'foo=bar&baz=quux&xyzzy=thu%64', - multicharsep: 'foo=bar&&&&&&&&&&baz=quux&&&&&&&&&&xyzzy=thud', - multivalue: 'foo=bar&foo=baz&foo=quux&quuy=quuz', - multivaluemany: 'foo=bar&foo=baz&foo=quux&quuy=quuz&foo=abc&foo=def&' + - 'foo=ghi&foo=jkl&foo=mno&foo=pqr&foo=stu&foo=vwxyz', - manypairs: 'a&b&c&d&e&f&g&h&i&j&k&l&m&n&o&p&q&r&s&t&u&v&w&x&y&z' -}; +const inputs = require('../fixtures/url-inputs.js').searchParams; const bench = common.createBenchmark(main, { type: Object.keys(inputs), diff --git a/benchmark/url/legacy-vs-whatwg-url-searchparams-serialize.js b/benchmark/url/legacy-vs-whatwg-url-searchparams-serialize.js index ef3160eccd49e6..7e56b5fba6e4f8 100644 --- a/benchmark/url/legacy-vs-whatwg-url-searchparams-serialize.js +++ b/benchmark/url/legacy-vs-whatwg-url-searchparams-serialize.js @@ -2,18 +2,7 @@ const common = require('../common.js'); const { URLSearchParams } = require('url'); const querystring = require('querystring'); - -const inputs = { - noencode: 'foo=bar&baz=quux&xyzzy=thud', - encodemany: '%66%6F%6F=bar&%62%61%7A=quux&xyzzy=%74h%75d', - encodefake: 'foo=%©ar&baz=%A©uux&xyzzy=%©ud', - encodelast: 'foo=bar&baz=quux&xyzzy=thu%64', - multicharsep: 'foo=bar&&&&&&&&&&baz=quux&&&&&&&&&&xyzzy=thud', - multivalue: 'foo=bar&foo=baz&foo=quux&quuy=quuz', - multivaluemany: 'foo=bar&foo=baz&foo=quux&quuy=quuz&foo=abc&foo=def&' + - 'foo=ghi&foo=jkl&foo=mno&foo=pqr&foo=stu&foo=vwxyz', - manypairs: 'a&b&c&d&e&f&g&h&i&j&k&l&m&n&o&p&q&r&s&t&u&v&w&x&y&z' -}; +const inputs = require('../fixtures/url-inputs.js').searchParams; const bench = common.createBenchmark(main, { type: Object.keys(inputs), diff --git a/benchmark/url/legacy-vs-whatwg-url-serialize.js b/benchmark/url/legacy-vs-whatwg-url-serialize.js index c0b7f5a6ce1565..911e79794b84fb 100644 --- a/benchmark/url/legacy-vs-whatwg-url-serialize.js +++ b/benchmark/url/legacy-vs-whatwg-url-serialize.js @@ -3,19 +3,7 @@ const common = require('../common.js'); const url = require('url'); const URL = url.URL; const assert = require('assert'); - -const inputs = { - long: 'http://nodejs.org:89/docs/latest/api/url.html#test?' + - 'payload1=true&payload2=false&test=1&benchmark=3&' + - 'foo=38.38.011.293&bar=1234834910480&test=19299&3992&' + - 'key=f5c65e1e98fe07e648249ad41e1cfdb0', - short: 'https://nodejs.org/en/blog/', - idn: 'http://你好你好', - auth: 'https://user:pass@example.com/path?search=1', - special: 'file:///foo/bar/test/node.js', - percent: 'https://%E4%BD%A0/foo', - dot: 'https://example.org/./a/../b/./c' -}; +const inputs = require('../fixtures/url-inputs.js').urls; const bench = common.createBenchmark(main, { type: Object.keys(inputs), From 93ee65178357137768618bbea2ed67a03d072d75 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 9 Feb 2017 23:41:42 +0800 Subject: [PATCH 2/3] benchmark: refactor whatwg-url-properties --- benchmark/url/whatwg-url-properties.js | 34 +++++++------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/benchmark/url/whatwg-url-properties.js b/benchmark/url/whatwg-url-properties.js index 375939c601d363..9bdc9778a8c922 100644 --- a/benchmark/url/whatwg-url-properties.js +++ b/benchmark/url/whatwg-url-properties.js @@ -1,16 +1,11 @@ 'use strict'; +const common = require('../common.js'); +const URL = require('url').URL; +const inputs = require('../fixtures/url-inputs.js').urls; -var common = require('../common.js'); -var URL = require('url').URL; - -var bench = common.createBenchmark(main, { - url: [ - 'http://example.com/', - 'https://encrypted.google.com/search?q=url&q=site:npmjs.org&hl=en', - 'javascript:alert("node is awesome");', - 'http://user:pass@foo.bar.com:21/aaa/zzz?l=24#test' - ], - prop: ['toString', 'href', 'origin', 'protocol', +const bench = common.createBenchmark(main, { + input: Object.keys(inputs), + prop: ['href', 'origin', 'protocol', 'username', 'password', 'host', 'hostname', 'port', 'pathname', 'search', 'searchParams', 'hash'], n: [1e4] @@ -34,14 +29,6 @@ function get(n, url, prop) { bench.end(n); } -function stringify(n, url, prop) { - bench.start(); - for (var i = 0; i < n; i += 1) { - url.toString(); - } - bench.end(n); -} - const alternatives = { href: 'http://user:pass@foo.bar.com:21/aaa/zzz?l=25#test', protocol: 'https:', @@ -61,7 +48,8 @@ function getAlternative(prop) { function main(conf) { const n = conf.n | 0; - const url = new URL(conf.url); + const input = inputs[conf.input]; + const url = new URL(input); const prop = conf.prop; switch (prop) { @@ -74,17 +62,13 @@ function main(conf) { case 'pathname': case 'search': case 'hash': + case 'href': setAndGet(n, url, prop, getAlternative(prop)); break; - // TODO: move href to the first group when the setter lands. - case 'href': case 'origin': case 'searchParams': get(n, url, prop); break; - case 'toString': - stringify(n, url); - break; default: throw new Error('Unknown prop'); } From b486814da1b527aacdb6220045720377d16f6e1e Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 9 Feb 2017 23:42:14 +0800 Subject: [PATCH 3/3] benchmark: clean up legacy url benchmarks --- benchmark/querystring/querystring-parse.js | 15 +------- benchmark/url/url-format.js | 11 +++--- benchmark/url/url-parse.js | 37 -------------------- benchmark/url/url-resolve.js | 40 +++++++++------------- 4 files changed, 23 insertions(+), 80 deletions(-) delete mode 100644 benchmark/url/url-parse.js diff --git a/benchmark/querystring/querystring-parse.js b/benchmark/querystring/querystring-parse.js index 0ed677751280c0..a44c3879bd63c0 100644 --- a/benchmark/querystring/querystring-parse.js +++ b/benchmark/querystring/querystring-parse.js @@ -2,20 +2,7 @@ var common = require('../common.js'); var querystring = require('querystring'); var v8 = require('v8'); - -var inputs = { - noencode: 'foo=bar&baz=quux&xyzzy=thud', - multicharsep: 'foo=bar&&&&&&&&&&baz=quux&&&&&&&&&&xyzzy=thud', - encodefake: 'foo=%©ar&baz=%A©uux&xyzzy=%©ud', - encodemany: '%66%6F%6F=bar&%62%61%7A=quux&xyzzy=%74h%75d', - encodelast: 'foo=bar&baz=quux&xyzzy=thu%64', - multivalue: 'foo=bar&foo=baz&foo=quux&quuy=quuz', - multivaluemany: 'foo=bar&foo=baz&foo=quux&quuy=quuz&foo=abc&foo=def&' + - 'foo=ghi&foo=jkl&foo=mno&foo=pqr&foo=stu&foo=vwxyz', - manypairs: 'a&b&c&d&e&f&g&h&i&j&k&l&m&n&o&p&q&r&s&t&u&v&w&x&y&z', - manyblankpairs: '&&&&&&&&&&&&&&&&&&&&&&&&', - altspaces: 'foo+bar=baz+quux&xyzzy+thud=quuy+quuz&abc=def+ghi' -}; +var inputs = require('../fixtures/url-inputs.js').searchParams; var bench = common.createBenchmark(main, { type: Object.keys(inputs), diff --git a/benchmark/url/url-format.js b/benchmark/url/url-format.js index 3f7df8a0bc4536..886958406b91c5 100644 --- a/benchmark/url/url-format.js +++ b/benchmark/url/url-format.js @@ -3,8 +3,13 @@ const common = require('../common.js'); const url = require('url'); const v8 = require('v8'); +const inputs = { + slashes: {slashes: true, host: 'localhost'}, + file: {protocol: 'file:', pathname: '/foo'}, +}; + const bench = common.createBenchmark(main, { - type: 'one two'.split(' '), + type: Object.keys(inputs), n: [25e6] }); @@ -12,10 +17,6 @@ function main(conf) { const type = conf.type; const n = conf.n | 0; - const inputs = { - one: {slashes: true, host: 'localhost'}, - two: {protocol: 'file:', pathname: '/foo'}, - }; const input = inputs[type] || ''; // Force-optimize url.format() so that the benchmark doesn't get diff --git a/benchmark/url/url-parse.js b/benchmark/url/url-parse.js deleted file mode 100644 index 89679548b8f193..00000000000000 --- a/benchmark/url/url-parse.js +++ /dev/null @@ -1,37 +0,0 @@ -'use strict'; -var common = require('../common.js'); -var url = require('url'); -var v8 = require('v8'); - -var bench = common.createBenchmark(main, { - type: 'one two three four five six'.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', - six: 'https://user:pass@example.com/', - }; - var input = inputs[type] || ''; - - // Force-optimize url.parse() so that the benchmark doesn't get - // disrupted by the optimizer kicking in halfway through. - for (var name in inputs) - url.parse(inputs[name]); - - v8.setFlagsFromString('--allow_natives_syntax'); - eval('%OptimizeFunctionOnNextCall(url.parse)'); - - bench.start(); - for (var i = 0; i < n; i += 1) - url.parse(input); - bench.end(n); -} diff --git a/benchmark/url/url-resolve.js b/benchmark/url/url-resolve.js index 8372132e4d269e..4335511ca6d2df 100644 --- a/benchmark/url/url-resolve.js +++ b/benchmark/url/url-resolve.js @@ -1,36 +1,28 @@ 'use strict'; -var common = require('../common.js'); -var url = require('url'); -var v8 = require('v8'); +const common = require('../common.js'); +const url = require('url'); +const v8 = require('v8'); +const hrefs = require('../fixtures/url-inputs.js').urls; +hrefs.noscheme = 'some.ran/dom/url.thing?oh=yes#whoo'; -var hrefs = [ - 'http://example.com/', - 'http://nodejs.org/docs/latest/api/url.html#url_url_format_urlobj', - 'http://blog.nodejs.org/', - 'https://encrypted.google.com/search?q=url&q=site:npmjs.org&hl=en', - 'javascript:alert("node is awesome");', - 'some.ran/dom/url.thing?oh=yes#whoo' -]; +const paths = { + 'up': '../../../../../etc/passwd', + 'sibling': '../foo/bar?baz=boom', + 'foo/bar': 'foo/bar', + 'withscheme': 'http://nodejs.org', + 'down': './foo/bar?baz' +}; - -var paths = [ - '../../../../../etc/passwd', - '../foo/bar?baz=boom', - 'foo/bar', - 'http://nodejs.org', - './foo/bar?baz' -]; - -var bench = common.createBenchmark(main, { +const bench = common.createBenchmark(main, { href: Object.keys(hrefs), path: Object.keys(paths), n: [1e5] }); function main(conf) { - var n = conf.n | 0; - var href = hrefs[conf.href]; - var path = paths[conf.path]; + const n = conf.n | 0; + const href = hrefs[conf.href]; + const path = paths[conf.path]; // Force-optimize url.resolve() so that the benchmark doesn't get // disrupted by the optimizer kicking in halfway through.