diff options
author | Timothy Gu <timothygu99@gmail.com> | 2016-11-25 02:55:56 +0300 |
---|---|---|
committer | Italo A. Casas <me@italoacasas.com> | 2017-01-27 16:12:07 +0300 |
commit | 5d33c9667997e749dded39013b2278345cbea072 (patch) | |
tree | b70c7f694836372f82c2f063fd462faa4fbe5c05 /benchmark | |
parent | 999f685a69c66856461f2576214bd62fd43e354a (diff) |
url: improving URLSearchParams
- add some benchmarks for URLSearchParams
- change URLSearchParams backing store to an array
- add custom inspection for URLSearchParams and its iterators
PR-URL: https://github.com/nodejs/node/pull/10399
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'benchmark')
-rw-r--r-- | benchmark/url/url-searchparams-iteration.js | 60 | ||||
-rw-r--r-- | benchmark/url/url-searchparams-parse.js | 31 | ||||
-rw-r--r-- | benchmark/url/url-searchparams-read.js | 58 | ||||
-rw-r--r-- | benchmark/url/url-searchparams-stringifier.js | 35 |
4 files changed, 184 insertions, 0 deletions
diff --git a/benchmark/url/url-searchparams-iteration.js b/benchmark/url/url-searchparams-iteration.js new file mode 100644 index 00000000000..caa4e45c412 --- /dev/null +++ b/benchmark/url/url-searchparams-iteration.js @@ -0,0 +1,60 @@ +'use strict'; +const common = require('../common.js'); +const assert = require('assert'); +const URLSearchParams = new (require('url').URL)('a:').searchParams.constructor; + +const bench = common.createBenchmark(main, { + method: ['forEach', 'iterator'], + n: [1e6] +}); + +const str = 'one=single&two=first&three=first&two=2nd&three=2nd&three=3rd'; + +function forEach(n) { + const params = new URLSearchParams(str); + const noDead = []; + const cb = (val, key) => { + noDead[0] = key; + noDead[1] = val; + }; + + bench.start(); + for (var i = 0; i < n; i += 1) + params.forEach(cb); + bench.end(n); + + assert.strictEqual(noDead[0], 'three'); + assert.strictEqual(noDead[1], '3rd'); +} + +function iterator(n) { + const params = new URLSearchParams(str); + const noDead = []; + + bench.start(); + for (var i = 0; i < n; i += 1) + for (var pair of params) { + noDead[0] = pair[0]; + noDead[1] = pair[1]; + } + bench.end(n); + + assert.strictEqual(noDead[0], 'three'); + assert.strictEqual(noDead[1], '3rd'); +} + +function main(conf) { + const method = conf.method; + const n = conf.n | 0; + + switch (method) { + case 'forEach': + forEach(n); + break; + case 'iterator': + iterator(n); + break; + default: + throw new Error('Unknown method'); + } +} diff --git a/benchmark/url/url-searchparams-parse.js b/benchmark/url/url-searchparams-parse.js new file mode 100644 index 00000000000..61796a7d327 --- /dev/null +++ b/benchmark/url/url-searchparams-parse.js @@ -0,0 +1,31 @@ +'use strict'; +const common = require('../common.js'); +const URLSearchParams = new (require('url').URL)('a:').searchParams.constructor; + +const inputs = { + noencode: 'foo=bar&baz=quux&xyzzy=thud', + // multicharsep: 'foo=bar&&&&&&&&&&baz=quux&&&&&&&&&&xyzzy=thud', + multicharsep: '&&&&&&&&&&&&&&&&&&&&&&&&&&&&', + 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' +}; + +const bench = common.createBenchmark(main, { + type: Object.keys(inputs), + n: [1e5] +}); + +function main(conf) { + const input = inputs[conf.type]; + const n = conf.n | 0; + + var i; + bench.start(); + for (i = 0; i < n; i++) + new URLSearchParams(input); + bench.end(n); +} diff --git a/benchmark/url/url-searchparams-read.js b/benchmark/url/url-searchparams-read.js new file mode 100644 index 00000000000..4cee49c74d2 --- /dev/null +++ b/benchmark/url/url-searchparams-read.js @@ -0,0 +1,58 @@ +'use strict'; +const common = require('../common.js'); +const URLSearchParams = new (require('url').URL)('a:').searchParams.constructor; + +const bench = common.createBenchmark(main, { + method: ['get', 'getAll', 'has'], + param: ['one', 'two', 'three', 'nonexistent'], + n: [1e6] +}); + +const str = 'one=single&two=first&three=first&two=2nd&three=2nd&three=3rd'; + +function get(n, param) { + const params = new URLSearchParams(str); + + bench.start(); + for (var i = 0; i < n; i += 1) + params.get(param); + bench.end(n); +} + +function getAll(n, param) { + const params = new URLSearchParams(str); + + bench.start(); + for (var i = 0; i < n; i += 1) + params.getAll(param); + bench.end(n); +} + +function has(n, param) { + const params = new URLSearchParams(str); + + bench.start(); + for (var i = 0; i < n; i += 1) + params.has(param); + bench.end(n); +} + +function main(conf) { + const method = conf.method; + const param = conf.param; + const n = conf.n | 0; + + switch (method) { + case 'get': + get(n, param); + break; + case 'getAll': + getAll(n, param); + break; + case 'has': + has(n, param); + break; + default: + throw new Error('Unknown method'); + } +} diff --git a/benchmark/url/url-searchparams-stringifier.js b/benchmark/url/url-searchparams-stringifier.js new file mode 100644 index 00000000000..2979064b322 --- /dev/null +++ b/benchmark/url/url-searchparams-stringifier.js @@ -0,0 +1,35 @@ +'use strict'; +const common = require('../common.js'); +const Buffer = require('buffer').Buffer; +const URLSearchParams = new (require('url').URL)('a:').searchParams.constructor; + +const inputs = { + noencode: 'foo=bar&baz=quux&xyzzy=thud', + // multicharsep: 'foo=bar&&&&&&&&&&baz=quux&&&&&&&&&&xyzzy=thud', + multicharsep: '&&&&&&&&&&&&&&&&&&&&&&&&&&&&', + 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' +}; + +const bench = common.createBenchmark(main, { + type: Object.keys(inputs), + n: [1e5] +}); + +function main(conf) { + const input = inputs[conf.type]; + const n = conf.n | 0; + + const params = new URLSearchParams(input); + + bench.start(); + // Using Buffer.from to prevent JS version from cheating with ropes instead + // of strings + for (var i = 0; i < n; i += 1) + Buffer.from(params.toString()); + bench.end(n); +} |