Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

benchmarks: fix benchmark for url #19084

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions benchmark/url/legacy-vs-whatwg-url-searchparams-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
const common = require('../common.js');
const { URLSearchParams } = require('url');
const querystring = require('querystring');
const inputs = require('../fixtures/url-inputs.js').searchParams;
const searchParams = require('../fixtures/url-inputs.js').searchParams;

const bench = common.createBenchmark(main, {
type: Object.keys(inputs),
searchParam: Object.keys(searchParams),
method: ['legacy', 'whatwg'],
n: [1e6]
});
Expand All @@ -19,27 +19,27 @@ function useLegacy(n, input) {
bench.end(n);
}

function useWHATWG(n, input) {
new URLSearchParams(input);
function useWHATWG(n, param) {
new URLSearchParams(param);
bench.start();
for (var i = 0; i < n; i += 1) {
new URLSearchParams(input);
new URLSearchParams(param);
}
bench.end(n);
}

function main({ type, n, method }) {
const input = inputs[type];
if (!input) {
throw new Error(`Unknown input type "${type}"`);
function main({ searchParam, n, method }) {
const param = searchParams[searchParam];
if (!param) {
throw new Error(`Unknown search parameter type "${searchParam}"`);
}

switch (method) {
case 'legacy':
useLegacy(n, input);
useLegacy(n, param);
break;
case 'whatwg':
useWHATWG(n, input);
useWHATWG(n, param);
break;
default:
throw new Error(`Unknown method ${method}`);
Expand Down
20 changes: 10 additions & 10 deletions benchmark/url/legacy-vs-whatwg-url-searchparams-serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
const common = require('../common.js');
const { URLSearchParams } = require('url');
const querystring = require('querystring');
const inputs = require('../fixtures/url-inputs.js').searchParams;
const searchParams = require('../fixtures/url-inputs.js').searchParams;

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

function useWHATWG(n, input, prop) {
const obj = new URLSearchParams(input);
function useWHATWG(n, param, prop) {
const obj = new URLSearchParams(param);
obj.toString();
bench.start();
for (var i = 0; i < n; i += 1) {
Expand All @@ -30,18 +30,18 @@ function useWHATWG(n, input, prop) {
bench.end(n);
}

function main({ type, n, method }) {
const input = inputs[type];
if (!input) {
throw new Error(`Unknown input type "${type}"`);
function main({ searchParam, n, method }) {
const param = searchParams[searchParam];
if (!param) {
throw new Error(`Unknown search parameter type "${searchParam}"`);
}

switch (method) {
case 'legacy':
useLegacy(n, input);
useLegacy(n, param);
break;
case 'whatwg':
useWHATWG(n, input);
useWHATWG(n, param);
break;
default:
throw new Error(`Unknown method ${method}`);
Expand Down
8 changes: 4 additions & 4 deletions benchmark/url/url-searchparams-iteration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const assert = require('assert');
const { URLSearchParams } = require('url');

const bench = common.createBenchmark(main, {
method: ['forEach', 'iterator'],
loopMethod: ['forEach', 'iterator'],
n: [1e6]
});

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

function main({ method, n }) {
switch (method) {
function main({ loopMethod, n }) {
switch (loopMethod) {
case 'forEach':
forEach(n);
break;
case 'iterator':
iterator(n);
break;
default:
throw new Error(`Unknown method ${method}`);
throw new Error(`Unknown method ${loopMethod}`);
}
}
11 changes: 5 additions & 6 deletions benchmark/url/url-searchparams-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ const common = require('../common.js');
const { URLSearchParams } = require('url');

const bench = common.createBenchmark(main, {
method: ['get', 'getAll', 'has'],
accessMethod: ['get', 'getAll', 'has'],
param: ['one', 'two', 'three', 'nonexistent'],
n: [2e7]
});

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

function main({ method, param, n }) {
function main({ accessMethod, param, n }) {
const params = new URLSearchParams(str);
const fn = params[method];
if (!fn)
throw new Error(`Unknown method ${method}`);
if (!params[accessMethod])
throw new Error(`Unknown method ${accessMethod}`);

bench.start();
for (var i = 0; i < n; i += 1)
fn(param);
params[accessMethod](param);
bench.end(n);
}
8 changes: 4 additions & 4 deletions benchmark/url/whatwg-url-idna.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const common = require('../common.js');
const { domainToASCII, domainToUnicode } = require('url');

const inputs = {
const domains = {
empty: {
ascii: '',
unicode: ''
Expand All @@ -26,13 +26,13 @@ const inputs = {
};

const bench = common.createBenchmark(main, {
input: Object.keys(inputs),
domain: Object.keys(domains),
to: ['ascii', 'unicode'],
n: [5e6]
});

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

bench.start();
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-benchmark-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

require('../common');

const runBenchmark = require('../common/benchmark');

runBenchmark('url',
[
'method=legacy',
'loopMethod=forEach',
'accessMethod=get',
'type=short',
'searchParam=noencode',
'href=short',
'input=short',
'domain=empty',
'path=up',
'to=ascii',
'prop=href',
'n=1',
],
{ NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });