1'use strict'; 2const common = require('../common.js'); 3 4const values = { 5 noencode: { 6 foo: 'bar', 7 baz: 'quux', 8 xyzzy: 'thud', 9 }, 10 encodemany: { 11 '\u0080\u0083\u0089': 'bar', 12 '\u008C\u008E\u0099': 'quux', 13 'xyzzy': '\u00A5q\u00A3r', 14 }, 15 encodelast: { 16 foo: 'bar', 17 baz: 'quux', 18 xyzzy: 'thu\u00AC', 19 }, 20 array: { 21 foo: [], 22 baz: ['bar'], 23 xyzzy: ['bar', 'quux', 'thud'], 24 }, 25 multiprimitives: { 26 foo: false, 27 bar: -13.37, 28 baz: 'baz', 29 }, 30}; 31 32function paramGenerator(paramType) { 33 const valueKeys = Object.keys(values); 34 switch (paramType) { 35 case 'string': 36 // Return the values object with all values as strings 37 return valueKeys.reduce((acc, key) => { 38 acc[key] = Object.keys(values[key]).reduce((acc, k, i) => { 39 acc += `${k}=${values[key][k]}${i < valueKeys.length - 1 ? '&' : ''}`; 40 return acc; 41 }, ''); 42 return acc; 43 }, {}); 44 case 'iterable': 45 // Return the values object with all values as iterable 46 return valueKeys.reduce((acc, key) => { 47 acc[key] = Object.keys(values[key]).reduce((acc, k) => { 48 acc.push([k, values[key][k]]); 49 return acc; 50 }, []); 51 return acc; 52 }, {}); 53 case 'object': 54 // Return the values object with all values as objects 55 return values; 56 default: 57 } 58} 59 60const bench = common.createBenchmark(main, { 61 type: ['noencode', 'encodemany', 'encodelast', 'array', 'multiprimitives'], 62 inputType: ['string', 'iterable', 'object'], 63 n: [1e6], 64}); 65 66function main({ n, type, inputType }) { 67 const inputs = paramGenerator(inputType); 68 const input = inputs[type]; 69 70 // Force optimization before starting the benchmark 71 for (const name in inputs) { 72 new URLSearchParams(inputs[name]); 73 } 74 75 bench.start(); 76 for (let i = 0; i < n; i += 1) 77 new URLSearchParams(input); 78 bench.end(n); 79} 80