0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/benchmark/url/legacy-vs-whatwg-url-searchparams-serialize.js
Alex Ramirez 0d1b14ac19 benchmark: swap var for let in url benchmarks
In benchmark url directory this changes for loops using var to let
when it applies for consistency

PR-URL: https://github.com/nodejs/node/pull/28867
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
Reviewed-By: Rich Trott <rtrott@gmail.com>
2019-07-30 09:37:48 -07:00

50 lines
1.1 KiB
JavaScript

'use strict';
const common = require('../common.js');
const { URLSearchParams } = require('url');
const querystring = require('querystring');
const searchParams = common.searchParams;
const bench = common.createBenchmark(main, {
searchParam: Object.keys(searchParams),
method: ['legacy', 'whatwg'],
n: [1e6]
});
function useLegacy(n, input, prop) {
const obj = querystring.parse(input);
querystring.stringify(obj);
bench.start();
for (let i = 0; i < n; i += 1) {
querystring.stringify(obj);
}
bench.end(n);
}
function useWHATWG(n, param, prop) {
const obj = new URLSearchParams(param);
obj.toString();
bench.start();
for (let i = 0; i < n; i += 1) {
obj.toString();
}
bench.end(n);
}
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, param);
break;
case 'whatwg':
useWHATWG(n, param);
break;
default:
throw new Error(`Unknown method ${method}`);
}
}