Skip to content

Commit 530b8a4

Browse files
dayninMylesBorins
authored andcommitted
benchmark: fix benchmark for url
Rename different parameters with the same names to make it possible to run tests for url benchmarks. PR-URL: #19084 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Khaidi Chu <[email protected]> Reviewed-By: Shingo Inoue <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent e4c320e commit 530b8a4

6 files changed

+56
-35
lines changed

benchmark/url/legacy-vs-whatwg-url-searchparams-parse.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
const common = require('../common.js');
33
const { URLSearchParams } = require('url');
44
const querystring = require('querystring');
5-
const inputs = require('../fixtures/url-inputs.js').searchParams;
5+
const searchParams = require('../fixtures/url-inputs.js').searchParams;
66

77
const bench = common.createBenchmark(main, {
8-
type: Object.keys(inputs),
8+
searchParam: Object.keys(searchParams),
99
method: ['legacy', 'whatwg'],
1010
n: [1e6]
1111
});
@@ -19,27 +19,27 @@ function useLegacy(n, input) {
1919
bench.end(n);
2020
}
2121

22-
function useWHATWG(n, input) {
23-
new URLSearchParams(input);
22+
function useWHATWG(n, param) {
23+
new URLSearchParams(param);
2424
bench.start();
2525
for (var i = 0; i < n; i += 1) {
26-
new URLSearchParams(input);
26+
new URLSearchParams(param);
2727
}
2828
bench.end(n);
2929
}
3030

31-
function main({ type, n, method }) {
32-
const input = inputs[type];
33-
if (!input) {
34-
throw new Error(`Unknown input type "${type}"`);
31+
function main({ searchParam, n, method }) {
32+
const param = searchParams[searchParam];
33+
if (!param) {
34+
throw new Error(`Unknown search parameter type "${searchParam}"`);
3535
}
3636

3737
switch (method) {
3838
case 'legacy':
39-
useLegacy(n, input);
39+
useLegacy(n, param);
4040
break;
4141
case 'whatwg':
42-
useWHATWG(n, input);
42+
useWHATWG(n, param);
4343
break;
4444
default:
4545
throw new Error(`Unknown method ${method}`);

benchmark/url/legacy-vs-whatwg-url-searchparams-serialize.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
const common = require('../common.js');
33
const { URLSearchParams } = require('url');
44
const querystring = require('querystring');
5-
const inputs = require('../fixtures/url-inputs.js').searchParams;
5+
const searchParams = require('../fixtures/url-inputs.js').searchParams;
66

77
const bench = common.createBenchmark(main, {
8-
type: Object.keys(inputs),
8+
searchParam: Object.keys(searchParams),
99
method: ['legacy', 'whatwg'],
1010
n: [1e6]
1111
});
@@ -20,8 +20,8 @@ function useLegacy(n, input, prop) {
2020
bench.end(n);
2121
}
2222

23-
function useWHATWG(n, input, prop) {
24-
const obj = new URLSearchParams(input);
23+
function useWHATWG(n, param, prop) {
24+
const obj = new URLSearchParams(param);
2525
obj.toString();
2626
bench.start();
2727
for (var i = 0; i < n; i += 1) {
@@ -30,18 +30,18 @@ function useWHATWG(n, input, prop) {
3030
bench.end(n);
3131
}
3232

33-
function main({ type, n, method }) {
34-
const input = inputs[type];
35-
if (!input) {
36-
throw new Error(`Unknown input type "${type}"`);
33+
function main({ searchParam, n, method }) {
34+
const param = searchParams[searchParam];
35+
if (!param) {
36+
throw new Error(`Unknown search parameter type "${searchParam}"`);
3737
}
3838

3939
switch (method) {
4040
case 'legacy':
41-
useLegacy(n, input);
41+
useLegacy(n, param);
4242
break;
4343
case 'whatwg':
44-
useWHATWG(n, input);
44+
useWHATWG(n, param);
4545
break;
4646
default:
4747
throw new Error(`Unknown method ${method}`);

benchmark/url/url-searchparams-iteration.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const assert = require('assert');
44
const { URLSearchParams } = require('url');
55

66
const bench = common.createBenchmark(main, {
7-
method: ['forEach', 'iterator'],
7+
loopMethod: ['forEach', 'iterator'],
88
n: [1e6]
99
});
1010

@@ -44,15 +44,15 @@ function iterator(n) {
4444
assert.strictEqual(noDead[1], '3rd');
4545
}
4646

47-
function main({ method, n }) {
48-
switch (method) {
47+
function main({ loopMethod, n }) {
48+
switch (loopMethod) {
4949
case 'forEach':
5050
forEach(n);
5151
break;
5252
case 'iterator':
5353
iterator(n);
5454
break;
5555
default:
56-
throw new Error(`Unknown method ${method}`);
56+
throw new Error(`Unknown method ${loopMethod}`);
5757
}
5858
}

benchmark/url/url-searchparams-read.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@ const common = require('../common.js');
33
const { URLSearchParams } = require('url');
44

55
const bench = common.createBenchmark(main, {
6-
method: ['get', 'getAll', 'has'],
6+
accessMethod: ['get', 'getAll', 'has'],
77
param: ['one', 'two', 'three', 'nonexistent'],
88
n: [2e7]
99
});
1010

1111
const str = 'one=single&two=first&three=first&two=2nd&three=2nd&three=3rd';
1212

13-
function main({ method, param, n }) {
13+
function main({ accessMethod, param, n }) {
1414
const params = new URLSearchParams(str);
15-
const fn = params[method];
16-
if (!fn)
17-
throw new Error(`Unknown method ${method}`);
15+
if (!params[accessMethod])
16+
throw new Error(`Unknown method ${accessMethod}`);
1817

1918
bench.start();
2019
for (var i = 0; i < n; i += 1)
21-
fn(param);
20+
params[accessMethod](param);
2221
bench.end(n);
2322
}

benchmark/url/whatwg-url-idna.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const common = require('../common.js');
33
const { domainToASCII, domainToUnicode } = require('url');
44

5-
const inputs = {
5+
const domains = {
66
empty: {
77
ascii: '',
88
unicode: ''
@@ -26,13 +26,13 @@ const inputs = {
2626
};
2727

2828
const bench = common.createBenchmark(main, {
29-
input: Object.keys(inputs),
29+
domain: Object.keys(domains),
3030
to: ['ascii', 'unicode'],
3131
n: [5e6]
3232
});
3333

34-
function main({ n, to, input }) {
35-
const value = inputs[input][to];
34+
function main({ n, to, domain }) {
35+
const value = domains[domain][to];
3636
const method = to === 'ascii' ? domainToASCII : domainToUnicode;
3737

3838
bench.start();

test/parallel/test-benchmark-url.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const runBenchmark = require('../common/benchmark');
6+
7+
runBenchmark('url',
8+
[
9+
'method=legacy',
10+
'loopMethod=forEach',
11+
'accessMethod=get',
12+
'type=short',
13+
'searchParam=noencode',
14+
'href=short',
15+
'input=short',
16+
'domain=empty',
17+
'path=up',
18+
'to=ascii',
19+
'prop=href',
20+
'n=1',
21+
],
22+
{ NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });

0 commit comments

Comments
 (0)