Skip to content

Commit b4f3a30

Browse files
joyeecheungitaloacasas
authored andcommitted
benchmark: URLSearchParams v.s. querystring
Add benchmarks to compare the performance between URLSearchParams and querystring, remove duplicate benchmarks. PR-URL: #11170 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Timothy Gu <[email protected]>
1 parent 6d2797b commit b4f3a30

4 files changed

+126
-66
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const { URLSearchParams } = require('url');
4+
const querystring = require('querystring');
5+
6+
const inputs = {
7+
noencode: 'foo=bar&baz=quux&xyzzy=thud',
8+
encodemany: '%66%6F%6F=bar&%62%61%7A=quux&xyzzy=%74h%75d',
9+
encodefake: 'foo=%©ar&baz=%A©uux&xyzzy=%©ud',
10+
encodelast: 'foo=bar&baz=quux&xyzzy=thu%64',
11+
multicharsep: 'foo=bar&&&&&&&&&&baz=quux&&&&&&&&&&xyzzy=thud',
12+
multivalue: 'foo=bar&foo=baz&foo=quux&quuy=quuz',
13+
multivaluemany: 'foo=bar&foo=baz&foo=quux&quuy=quuz&foo=abc&foo=def&' +
14+
'foo=ghi&foo=jkl&foo=mno&foo=pqr&foo=stu&foo=vwxyz',
15+
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'
16+
};
17+
18+
const bench = common.createBenchmark(main, {
19+
type: Object.keys(inputs),
20+
method: ['legacy', 'whatwg'],
21+
n: [1e5]
22+
});
23+
24+
function useLegacy(n, input) {
25+
querystring.parse(input);
26+
bench.start();
27+
for (var i = 0; i < n; i += 1) {
28+
querystring.parse(input);
29+
}
30+
bench.end(n);
31+
}
32+
33+
function useWHATWG(n, input) {
34+
new URLSearchParams(input);
35+
bench.start();
36+
for (var i = 0; i < n; i += 1) {
37+
new URLSearchParams(input);
38+
}
39+
bench.end(n);
40+
}
41+
42+
function main(conf) {
43+
const type = conf.type;
44+
const n = conf.n | 0;
45+
const method = conf.method;
46+
47+
const input = inputs[type];
48+
if (!input) {
49+
throw new Error('Unknown input type');
50+
}
51+
52+
switch (method) {
53+
case 'legacy':
54+
useLegacy(n, input);
55+
break;
56+
case 'whatwg':
57+
useWHATWG(n, input);
58+
break;
59+
default:
60+
throw new Error('Unknown method');
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const { URLSearchParams } = require('url');
4+
const querystring = require('querystring');
5+
6+
const inputs = {
7+
noencode: 'foo=bar&baz=quux&xyzzy=thud',
8+
encodemany: '%66%6F%6F=bar&%62%61%7A=quux&xyzzy=%74h%75d',
9+
encodefake: 'foo=%©ar&baz=%A©uux&xyzzy=%©ud',
10+
encodelast: 'foo=bar&baz=quux&xyzzy=thu%64',
11+
multicharsep: 'foo=bar&&&&&&&&&&baz=quux&&&&&&&&&&xyzzy=thud',
12+
multivalue: 'foo=bar&foo=baz&foo=quux&quuy=quuz',
13+
multivaluemany: 'foo=bar&foo=baz&foo=quux&quuy=quuz&foo=abc&foo=def&' +
14+
'foo=ghi&foo=jkl&foo=mno&foo=pqr&foo=stu&foo=vwxyz',
15+
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'
16+
};
17+
18+
const bench = common.createBenchmark(main, {
19+
type: Object.keys(inputs),
20+
method: ['legacy', 'whatwg'],
21+
n: [1e5]
22+
});
23+
24+
function useLegacy(n, input, prop) {
25+
const obj = querystring.parse(input);
26+
querystring.stringify(obj);
27+
bench.start();
28+
for (var i = 0; i < n; i += 1) {
29+
querystring.stringify(obj);
30+
}
31+
bench.end(n);
32+
}
33+
34+
function useWHATWG(n, input, prop) {
35+
const obj = new URLSearchParams(input);
36+
obj.toString();
37+
bench.start();
38+
for (var i = 0; i < n; i += 1) {
39+
obj.toString();
40+
}
41+
bench.end(n);
42+
}
43+
44+
function main(conf) {
45+
const type = conf.type;
46+
const n = conf.n | 0;
47+
const method = conf.method;
48+
49+
const input = inputs[type];
50+
if (!input) {
51+
throw new Error('Unknown input type');
52+
}
53+
54+
switch (method) {
55+
case 'legacy':
56+
useLegacy(n, input);
57+
break;
58+
case 'whatwg':
59+
useWHATWG(n, input);
60+
break;
61+
default:
62+
throw new Error('Unknown method');
63+
}
64+
}

benchmark/url/url-searchparams-parse.js

-31
This file was deleted.

benchmark/url/url-searchparams-stringifier.js

-35
This file was deleted.

0 commit comments

Comments
 (0)