11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common.js');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst values = {
51cb0ef41Sopenharmony_ci  noencode: {
61cb0ef41Sopenharmony_ci    foo: 'bar',
71cb0ef41Sopenharmony_ci    baz: 'quux',
81cb0ef41Sopenharmony_ci    xyzzy: 'thud',
91cb0ef41Sopenharmony_ci  },
101cb0ef41Sopenharmony_ci  encodemany: {
111cb0ef41Sopenharmony_ci    '\u0080\u0083\u0089': 'bar',
121cb0ef41Sopenharmony_ci    '\u008C\u008E\u0099': 'quux',
131cb0ef41Sopenharmony_ci    'xyzzy': '\u00A5q\u00A3r',
141cb0ef41Sopenharmony_ci  },
151cb0ef41Sopenharmony_ci  encodelast: {
161cb0ef41Sopenharmony_ci    foo: 'bar',
171cb0ef41Sopenharmony_ci    baz: 'quux',
181cb0ef41Sopenharmony_ci    xyzzy: 'thu\u00AC',
191cb0ef41Sopenharmony_ci  },
201cb0ef41Sopenharmony_ci  array: {
211cb0ef41Sopenharmony_ci    foo: [],
221cb0ef41Sopenharmony_ci    baz: ['bar'],
231cb0ef41Sopenharmony_ci    xyzzy: ['bar', 'quux', 'thud'],
241cb0ef41Sopenharmony_ci  },
251cb0ef41Sopenharmony_ci  multiprimitives: {
261cb0ef41Sopenharmony_ci    foo: false,
271cb0ef41Sopenharmony_ci    bar: -13.37,
281cb0ef41Sopenharmony_ci    baz: 'baz',
291cb0ef41Sopenharmony_ci  },
301cb0ef41Sopenharmony_ci};
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_cifunction paramGenerator(paramType) {
331cb0ef41Sopenharmony_ci  const valueKeys = Object.keys(values);
341cb0ef41Sopenharmony_ci  switch (paramType) {
351cb0ef41Sopenharmony_ci    case 'string':
361cb0ef41Sopenharmony_ci      // Return the values object with all values as strings
371cb0ef41Sopenharmony_ci      return valueKeys.reduce((acc, key) => {
381cb0ef41Sopenharmony_ci        const objectKeys = Object.keys(values[key]);
391cb0ef41Sopenharmony_ci        acc[key] = objectKeys.reduce((acc, k, i) => {
401cb0ef41Sopenharmony_ci          acc += `${k}=${values[key][k]}${i < objectKeys.length - 1 ? '&' : ''}`;
411cb0ef41Sopenharmony_ci          return acc;
421cb0ef41Sopenharmony_ci        }, '');
431cb0ef41Sopenharmony_ci        return acc;
441cb0ef41Sopenharmony_ci      }, {});
451cb0ef41Sopenharmony_ci    case 'iterable':
461cb0ef41Sopenharmony_ci      // Return the values object with all values as iterable
471cb0ef41Sopenharmony_ci      return valueKeys.reduce((acc, key) => {
481cb0ef41Sopenharmony_ci        acc[key] = Object.keys(values[key]).reduce((acc, k) => {
491cb0ef41Sopenharmony_ci          acc.push([k, values[key][k]]);
501cb0ef41Sopenharmony_ci          return acc;
511cb0ef41Sopenharmony_ci        }, []);
521cb0ef41Sopenharmony_ci        return acc;
531cb0ef41Sopenharmony_ci      }, {});
541cb0ef41Sopenharmony_ci    case 'object':
551cb0ef41Sopenharmony_ci      // Return the values object with all values as objects
561cb0ef41Sopenharmony_ci      return values;
571cb0ef41Sopenharmony_ci    default:
581cb0ef41Sopenharmony_ci  }
591cb0ef41Sopenharmony_ci}
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
621cb0ef41Sopenharmony_ci  type: ['noencode', 'encodemany', 'encodelast', 'array', 'multiprimitives'],
631cb0ef41Sopenharmony_ci  inputType: ['string', 'iterable', 'object'],
641cb0ef41Sopenharmony_ci  n: [1e6],
651cb0ef41Sopenharmony_ci});
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_cifunction main({ n, type, inputType }) {
681cb0ef41Sopenharmony_ci  const inputs = paramGenerator(inputType);
691cb0ef41Sopenharmony_ci  const input = inputs[type];
701cb0ef41Sopenharmony_ci  const u = new URLSearchParams(input);
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  bench.start();
731cb0ef41Sopenharmony_ci  for (let i = 0; i < n; i += 1)
741cb0ef41Sopenharmony_ci    u.toString();
751cb0ef41Sopenharmony_ci  bench.end(n);
761cb0ef41Sopenharmony_ci}
77