Skip to content

Commit 7cfd31a

Browse files
debadree25targos
authored andcommitted
benchmark: add a benchmark for URLSearchParams creation and toString()
Refs: nodejs/performance#56 PR-URL: #46810 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 2b7eb56 commit 7cfd31a

2 files changed

Lines changed: 155 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'use strict';
2+
constcommon=require('../common.js');
3+
4+
constvalues={
5+
noencode: {
6+
foo: 'bar',
7+
baz: 'quux',
8+
xyzzy: 'thud',
9+
},
10+
encodemany: {
11+
'\u0080\u0083\u0089': 'bar',
12+
'\u008C\u008E\u0099': 'quux',
13+
'xyzzy': '\u00A5q\u00A3r',
14+
},
15+
encodelast: {
16+
foo: 'bar',
17+
baz: 'quux',
18+
xyzzy: 'thu\u00AC',
19+
},
20+
array: {
21+
foo: [],
22+
baz: ['bar'],
23+
xyzzy: ['bar','quux','thud'],
24+
},
25+
multiprimitives: {
26+
foo: false,
27+
bar: -13.37,
28+
baz: 'baz',
29+
},
30+
};
31+
32+
functionparamGenerator(paramType){
33+
constvalueKeys=Object.keys(values);
34+
switch(paramType){
35+
case'string':
36+
// Return the values object with all values as strings
37+
returnvalueKeys.reduce((acc,key)=>{
38+
acc[key]=Object.keys(values[key]).reduce((acc,k,i)=>{
39+
acc+=`${k}=${values[key][k]}${i<valueKeys.length-1 ? '&' : ''}`;
40+
returnacc;
41+
},'');
42+
returnacc;
43+
},{});
44+
case'iterable':
45+
// Return the values object with all values as iterable
46+
returnvalueKeys.reduce((acc,key)=>{
47+
acc[key]=Object.keys(values[key]).reduce((acc,k)=>{
48+
acc.push([k,values[key][k]]);
49+
returnacc;
50+
},[]);
51+
returnacc;
52+
},{});
53+
case'object':
54+
// Return the values object with all values as objects
55+
returnvalues;
56+
default:
57+
}
58+
}
59+
60+
constbench=common.createBenchmark(main,{
61+
type: ['noencode','encodemany','encodelast','array','multiprimitives'],
62+
inputType: ['string','iterable','object'],
63+
n: [1e6],
64+
});
65+
66+
functionmain({ n, type, inputType }){
67+
constinputs=paramGenerator(inputType);
68+
constinput=inputs[type];
69+
70+
// Force optimization before starting the benchmark
71+
for(constnameininputs){
72+
newURLSearchParams(inputs[name]);
73+
}
74+
75+
bench.start();
76+
for(leti=0;i<n;i+=1)
77+
newURLSearchParams(input);
78+
bench.end(n);
79+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'use strict';
2+
constcommon=require('../common.js');
3+
4+
constvalues={
5+
noencode: {
6+
foo: 'bar',
7+
baz: 'quux',
8+
xyzzy: 'thud',
9+
},
10+
encodemany: {
11+
'\u0080\u0083\u0089': 'bar',
12+
'\u008C\u008E\u0099': 'quux',
13+
'xyzzy': '\u00A5q\u00A3r',
14+
},
15+
encodelast: {
16+
foo: 'bar',
17+
baz: 'quux',
18+
xyzzy: 'thu\u00AC',
19+
},
20+
array: {
21+
foo: [],
22+
baz: ['bar'],
23+
xyzzy: ['bar','quux','thud'],
24+
},
25+
multiprimitives: {
26+
foo: false,
27+
bar: -13.37,
28+
baz: 'baz',
29+
},
30+
};
31+
32+
functionparamGenerator(paramType){
33+
constvalueKeys=Object.keys(values);
34+
switch(paramType){
35+
case'string':
36+
// Return the values object with all values as strings
37+
returnvalueKeys.reduce((acc,key)=>{
38+
constobjectKeys=Object.keys(values[key]);
39+
acc[key]=objectKeys.reduce((acc,k,i)=>{
40+
acc+=`${k}=${values[key][k]}${i<objectKeys.length-1 ? '&' : ''}`;
41+
returnacc;
42+
},'');
43+
returnacc;
44+
},{});
45+
case'iterable':
46+
// Return the values object with all values as iterable
47+
returnvalueKeys.reduce((acc,key)=>{
48+
acc[key]=Object.keys(values[key]).reduce((acc,k)=>{
49+
acc.push([k,values[key][k]]);
50+
returnacc;
51+
},[]);
52+
returnacc;
53+
},{});
54+
case'object':
55+
// Return the values object with all values as objects
56+
returnvalues;
57+
default:
58+
}
59+
}
60+
61+
constbench=common.createBenchmark(main,{
62+
type: ['noencode','encodemany','encodelast','array','multiprimitives'],
63+
inputType: ['string','iterable','object'],
64+
n: [1e6],
65+
});
66+
67+
functionmain({ n, type, inputType }){
68+
constinputs=paramGenerator(inputType);
69+
constinput=inputs[type];
70+
constu=newURLSearchParams(input);
71+
72+
bench.start();
73+
for(leti=0;i<n;i+=1)
74+
u.toString();
75+
bench.end(n);
76+
}

0 commit comments

Comments
 (0)