Skip to content

Commit 5d33c96

Browse files
TimothyGuitaloacasas
authored andcommitted
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: #10399 Reviewed-By: James M Snell <[email protected]>
1 parent 999f685 commit 5d33c96

6 files changed

+369
-43
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const assert = require('assert');
4+
const URLSearchParams = new (require('url').URL)('a:').searchParams.constructor;
5+
6+
const bench = common.createBenchmark(main, {
7+
method: ['forEach', 'iterator'],
8+
n: [1e6]
9+
});
10+
11+
const str = 'one=single&two=first&three=first&two=2nd&three=2nd&three=3rd';
12+
13+
function forEach(n) {
14+
const params = new URLSearchParams(str);
15+
const noDead = [];
16+
const cb = (val, key) => {
17+
noDead[0] = key;
18+
noDead[1] = val;
19+
};
20+
21+
bench.start();
22+
for (var i = 0; i < n; i += 1)
23+
params.forEach(cb);
24+
bench.end(n);
25+
26+
assert.strictEqual(noDead[0], 'three');
27+
assert.strictEqual(noDead[1], '3rd');
28+
}
29+
30+
function iterator(n) {
31+
const params = new URLSearchParams(str);
32+
const noDead = [];
33+
34+
bench.start();
35+
for (var i = 0; i < n; i += 1)
36+
for (var pair of params) {
37+
noDead[0] = pair[0];
38+
noDead[1] = pair[1];
39+
}
40+
bench.end(n);
41+
42+
assert.strictEqual(noDead[0], 'three');
43+
assert.strictEqual(noDead[1], '3rd');
44+
}
45+
46+
function main(conf) {
47+
const method = conf.method;
48+
const n = conf.n | 0;
49+
50+
switch (method) {
51+
case 'forEach':
52+
forEach(n);
53+
break;
54+
case 'iterator':
55+
iterator(n);
56+
break;
57+
default:
58+
throw new Error('Unknown method');
59+
}
60+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const URLSearchParams = new (require('url').URL)('a:').searchParams.constructor;
4+
5+
const inputs = {
6+
noencode: 'foo=bar&baz=quux&xyzzy=thud',
7+
// multicharsep: 'foo=bar&&&&&&&&&&baz=quux&&&&&&&&&&xyzzy=thud',
8+
multicharsep: '&&&&&&&&&&&&&&&&&&&&&&&&&&&&',
9+
encodemany: '%66%6F%6F=bar&%62%61%7A=quux&xyzzy=%74h%75d',
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+
n: [1e5]
20+
});
21+
22+
function main(conf) {
23+
const input = inputs[conf.type];
24+
const n = conf.n | 0;
25+
26+
var i;
27+
bench.start();
28+
for (i = 0; i < n; i++)
29+
new URLSearchParams(input);
30+
bench.end(n);
31+
}
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const URLSearchParams = new (require('url').URL)('a:').searchParams.constructor;
4+
5+
const bench = common.createBenchmark(main, {
6+
method: ['get', 'getAll', 'has'],
7+
param: ['one', 'two', 'three', 'nonexistent'],
8+
n: [1e6]
9+
});
10+
11+
const str = 'one=single&two=first&three=first&two=2nd&three=2nd&three=3rd';
12+
13+
function get(n, param) {
14+
const params = new URLSearchParams(str);
15+
16+
bench.start();
17+
for (var i = 0; i < n; i += 1)
18+
params.get(param);
19+
bench.end(n);
20+
}
21+
22+
function getAll(n, param) {
23+
const params = new URLSearchParams(str);
24+
25+
bench.start();
26+
for (var i = 0; i < n; i += 1)
27+
params.getAll(param);
28+
bench.end(n);
29+
}
30+
31+
function has(n, param) {
32+
const params = new URLSearchParams(str);
33+
34+
bench.start();
35+
for (var i = 0; i < n; i += 1)
36+
params.has(param);
37+
bench.end(n);
38+
}
39+
40+
function main(conf) {
41+
const method = conf.method;
42+
const param = conf.param;
43+
const n = conf.n | 0;
44+
45+
switch (method) {
46+
case 'get':
47+
get(n, param);
48+
break;
49+
case 'getAll':
50+
getAll(n, param);
51+
break;
52+
case 'has':
53+
has(n, param);
54+
break;
55+
default:
56+
throw new Error('Unknown method');
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const Buffer = require('buffer').Buffer;
4+
const URLSearchParams = new (require('url').URL)('a:').searchParams.constructor;
5+
6+
const inputs = {
7+
noencode: 'foo=bar&baz=quux&xyzzy=thud',
8+
// multicharsep: 'foo=bar&&&&&&&&&&baz=quux&&&&&&&&&&xyzzy=thud',
9+
multicharsep: '&&&&&&&&&&&&&&&&&&&&&&&&&&&&',
10+
encodemany: '%66%6F%6F=bar&%62%61%7A=quux&xyzzy=%74h%75d',
11+
encodelast: 'foo=bar&baz=quux&xyzzy=thu%64',
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+
n: [1e5]
21+
});
22+
23+
function main(conf) {
24+
const input = inputs[conf.type];
25+
const n = conf.n | 0;
26+
27+
const params = new URLSearchParams(input);
28+
29+
bench.start();
30+
// Using Buffer.from to prevent JS version from cheating with ropes instead
31+
// of strings
32+
for (var i = 0; i < n; i += 1)
33+
Buffer.from(params.toString());
34+
bench.end(n);
35+
}

0 commit comments

Comments
 (0)