diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2017-02-04 23:31:45 +0300 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2017-02-09 17:57:53 +0300 |
commit | ab3e0862af61d6713503eb3138adb9aca33851a3 (patch) | |
tree | 2887ba56fa86b8ff38267842cb849a295ae08785 /benchmark | |
parent | c6b586de33b6ac208d3de83b304bfe50f13a4292 (diff) |
benchmark: URLSearchParams v.s. querystring
Add benchmarks to compare the performance between
URLSearchParams and querystring, remove duplicate
benchmarks.
PR-URL: https://github.com/nodejs/node/pull/11170
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Diffstat (limited to 'benchmark')
-rw-r--r-- | benchmark/url/legacy-vs-whatwg-url-searchparams-parse.js | 62 | ||||
-rw-r--r-- | benchmark/url/legacy-vs-whatwg-url-searchparams-serialize.js | 64 | ||||
-rw-r--r-- | benchmark/url/url-searchparams-parse.js | 31 | ||||
-rw-r--r-- | benchmark/url/url-searchparams-stringifier.js | 35 |
4 files changed, 126 insertions, 66 deletions
diff --git a/benchmark/url/legacy-vs-whatwg-url-searchparams-parse.js b/benchmark/url/legacy-vs-whatwg-url-searchparams-parse.js new file mode 100644 index 00000000000..e76f2dd837b --- /dev/null +++ b/benchmark/url/legacy-vs-whatwg-url-searchparams-parse.js @@ -0,0 +1,62 @@ +'use strict'; +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 bench = common.createBenchmark(main, { + type: Object.keys(inputs), + method: ['legacy', 'whatwg'], + n: [1e5] +}); + +function useLegacy(n, input) { + querystring.parse(input); + bench.start(); + for (var i = 0; i < n; i += 1) { + querystring.parse(input); + } + bench.end(n); +} + +function useWHATWG(n, input) { + new URLSearchParams(input); + bench.start(); + for (var i = 0; i < n; i += 1) { + new URLSearchParams(input); + } + bench.end(n); +} + +function main(conf) { + const type = conf.type; + const n = conf.n | 0; + const method = conf.method; + + const input = inputs[type]; + if (!input) { + throw new Error('Unknown input type'); + } + + switch (method) { + case 'legacy': + useLegacy(n, input); + break; + case 'whatwg': + useWHATWG(n, input); + break; + default: + throw new Error('Unknown method'); + } +} diff --git a/benchmark/url/legacy-vs-whatwg-url-searchparams-serialize.js b/benchmark/url/legacy-vs-whatwg-url-searchparams-serialize.js new file mode 100644 index 00000000000..ef3160eccd4 --- /dev/null +++ b/benchmark/url/legacy-vs-whatwg-url-searchparams-serialize.js @@ -0,0 +1,64 @@ +'use strict'; +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 bench = common.createBenchmark(main, { + type: Object.keys(inputs), + method: ['legacy', 'whatwg'], + n: [1e5] +}); + +function useLegacy(n, input, prop) { + const obj = querystring.parse(input); + querystring.stringify(obj); + bench.start(); + for (var i = 0; i < n; i += 1) { + querystring.stringify(obj); + } + bench.end(n); +} + +function useWHATWG(n, input, prop) { + const obj = new URLSearchParams(input); + obj.toString(); + bench.start(); + for (var i = 0; i < n; i += 1) { + obj.toString(); + } + bench.end(n); +} + +function main(conf) { + const type = conf.type; + const n = conf.n | 0; + const method = conf.method; + + const input = inputs[type]; + if (!input) { + throw new Error('Unknown input type'); + } + + switch (method) { + case 'legacy': + useLegacy(n, input); + break; + case 'whatwg': + useWHATWG(n, input); + break; + default: + throw new Error('Unknown method'); + } +} diff --git a/benchmark/url/url-searchparams-parse.js b/benchmark/url/url-searchparams-parse.js deleted file mode 100644 index c156e452e1b..00000000000 --- a/benchmark/url/url-searchparams-parse.js +++ /dev/null @@ -1,31 +0,0 @@ -'use strict'; -const common = require('../common.js'); -const { URLSearchParams } = require('url'); - -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-stringifier.js b/benchmark/url/url-searchparams-stringifier.js deleted file mode 100644 index 1a2f6777276..00000000000 --- a/benchmark/url/url-searchparams-stringifier.js +++ /dev/null @@ -1,35 +0,0 @@ -'use strict'; -const common = require('../common.js'); -const Buffer = require('buffer').Buffer; -const { URLSearchParams } = require('url'); - -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); -} |