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-parse.js
Joyee Cheung a365bb9cb1
benchmark: pre-generate data set for URL benchmarks
This patch:

- Introduces `common.bakeUrlData` which can be used to pre-generate
  the data set for the URL benchmarks to loop through instead of
  looping over a constant.
- Add the option to use WPT data in benchmarks for better diversity
  in the input
- Add the option to benchmark URL parsing with base URLs (whatwg only)
- Moves the data in `benchmark/fixtures/url-inputs.js` to
  `benchmark/common.js`

PR-URL: https://github.com/nodejs/node/pull/24302
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2018-11-20 05:14:34 +08:00

48 lines
1.0 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) {
querystring.parse(input);
bench.start();
for (var i = 0; i < n; i += 1) {
querystring.parse(input);
}
bench.end(n);
}
function useWHATWG(n, param) {
new URLSearchParams(param);
bench.start();
for (var i = 0; i < n; i += 1) {
new URLSearchParams(param);
}
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}`);
}
}