Skip to content

Commit 56e4d2c

Browse files
committed
benchmark: URLSearchParams v.s. querystring
Add benchmarks to compare the performance between URLSearchParams and querystring, remove duplicate benchmarks.
1 parent 98166ff commit 56e4d2c

4 files changed

Lines changed: 124 additions & 66 deletions
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
multivalue: 'foo=bar&foo=baz&foo=quux&quuy=quuz',
12+
multivaluemany: 'foo=bar&foo=baz&foo=quux&quuy=quuz&foo=abc&foo=def&' +
13+
'foo=ghi&foo=jkl&foo=mno&foo=pqr&foo=stu&foo=vwxyz',
14+
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'
15+
};
16+
17+
const bench = common.createBenchmark(main, {
18+
type: Object.keys(inputs),
19+
method: ['legacy', 'whatwg'],
20+
n: [1e5]
21+
});
22+
23+
function useLegacy(n, input) {
24+
querystring.parse(input);
25+
bench.start();
26+
for (var i = 0; i < n; i += 1) {
27+
querystring.parse(input);
28+
}
29+
bench.end(n);
30+
}
31+
32+
function useWHATWG(n, input) {
33+
new URLSearchParams(input);
34+
bench.start();
35+
for (var i = 0; i < n; i += 1) {
36+
new URLSearchParams(input);
37+
}
38+
bench.end(n);
39+
}
40+
41+
function main(conf) {
42+
const type = conf.type;
43+
const n = conf.n | 0;
44+
const method = conf.method;
45+
46+
const input = inputs[type];
47+
if (!input) {
48+
throw new Error('Unknown input type');
49+
}
50+
51+
switch (method) {
52+
case 'legacy':
53+
useLegacy(n, input);
54+
break;
55+
case 'whatwg':
56+
useWHATWG(n, input);
57+
break;
58+
default:
59+
throw new Error('Unknown method');
60+
}
61+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
multivalue: 'foo=bar&foo=baz&foo=quux&quuy=quuz',
12+
multivaluemany: 'foo=bar&foo=baz&foo=quux&quuy=quuz&foo=abc&foo=def&' +
13+
'foo=ghi&foo=jkl&foo=mno&foo=pqr&foo=stu&foo=vwxyz',
14+
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'
15+
};
16+
17+
const bench = common.createBenchmark(main, {
18+
type: Object.keys(inputs),
19+
method: ['legacy', 'whatwg'],
20+
n: [1e5]
21+
});
22+
23+
function useLegacy(n, input, prop) {
24+
const obj = querystring.parse(input);
25+
querystring.stringify(obj);
26+
bench.start();
27+
for (var i = 0; i < n; i += 1) {
28+
querystring.stringify(obj);
29+
}
30+
bench.end(n);
31+
}
32+
33+
function useWHATWG(n, input, prop) {
34+
const obj = new URLSearchParams(input);
35+
obj.toString();
36+
bench.start();
37+
for (var i = 0; i < n; i += 1) {
38+
obj.toString();
39+
}
40+
bench.end(n);
41+
}
42+
43+
function main(conf) {
44+
const type = conf.type;
45+
const n = conf.n | 0;
46+
const method = conf.method;
47+
48+
const input = inputs[type];
49+
if (!input) {
50+
throw new Error('Unknown input type');
51+
}
52+
53+
switch (method) {
54+
case 'legacy':
55+
useLegacy(n, input);
56+
break;
57+
case 'whatwg':
58+
useWHATWG(n, input);
59+
break;
60+
default:
61+
throw new Error('Unknown method');
62+
}
63+
}

benchmark/url/url-searchparams-parse.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

benchmark/url/url-searchparams-stringifier.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)