summaryrefslogtreecommitdiff
path: root/benchmark/url/url-searchparams-read.js
blob: 0cf66dabbc36dcfe435e5da3ea6f55c3996ddfac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
'use strict';
const common = require('../common.js');
const { URLSearchParams } = require('url');

const bench = common.createBenchmark(main, {
  method: ['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 }) {
  const params = new URLSearchParams(str);
  const fn = params[method];
  if (!fn)
    throw new Error(`Unknown method ${method}`);

  bench.start();
  for (var i = 0; i < n; i += 1)
    fn(param);
  bench.end(n);
}