• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common.js');
3const querystring = require('querystring');
4const searchParams = common.searchParams;
5
6const bench = common.createBenchmark(main, {
7  searchParam: Object.keys(searchParams),
8  method: ['legacy', 'whatwg'],
9  n: [1e6],
10});
11
12function useLegacy(n, input, prop) {
13  const obj = querystring.parse(input);
14  querystring.stringify(obj);
15  bench.start();
16  for (let i = 0; i < n; i += 1) {
17    querystring.stringify(obj);
18  }
19  bench.end(n);
20}
21
22function useWHATWG(n, param, prop) {
23  const obj = new URLSearchParams(param);
24  obj.toString();
25  bench.start();
26  for (let i = 0; i < n; i += 1) {
27    obj.toString();
28  }
29  bench.end(n);
30}
31
32function main({ searchParam, n, method }) {
33  const param = searchParams[searchParam];
34  if (!param) {
35    throw new Error(`Unknown search parameter type "${searchParam}"`);
36  }
37
38  switch (method) {
39    case 'legacy':
40      useLegacy(n, param);
41      break;
42    case 'whatwg':
43      useWHATWG(n, param);
44      break;
45    default:
46      throw new Error(`Unknown method ${method}`);
47  }
48}
49