-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.js
75 lines (62 loc) · 2.62 KB
/
benchmark.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'use strict';
const Benchmark = require('benchmark');
const queryString = require('.');
const {stringify, stringifyUrl} = queryString;
const suite = new Benchmark.Suite();
// Fixtures
const TEST_OBJECT = {
genre: 'Epic fantasy',
author: '',
page: 2,
published: true,
symbols: 'πµ',
chapters: [1, 2, 3],
none: null
};
const TEST_HOST = 'https://foo.bar/';
const TEST_STRING = stringify(TEST_OBJECT);
const TEST_BRACKETS_STRING = stringify(TEST_OBJECT, {arrayFormat: 'bracket'});
const TEST_INDEX_STRING = stringify(TEST_OBJECT, {arrayFormat: 'index'});
const TEST_COMMA_STRING = stringify(TEST_OBJECT, {arrayFormat: 'comma'});
const TEST_BRACKET_SEPARATOR_STRING = stringify(TEST_OBJECT, {arrayFormat: 'bracket-separator'});
const TEST_URL = stringifyUrl({url: TEST_HOST, query: TEST_OBJECT});
// Creates a test case and adds it to the suite
const defineTestCase = (methodName, input, options) => {
const fn = queryString[methodName];
const label = options ? ` (${stringify(options)})` : '';
suite.add(methodName + label, () => fn(input, options || {}));
};
// Define all test cases
// Parse
defineTestCase('parse', TEST_STRING);
defineTestCase('parse', TEST_STRING, {parseNumbers: true});
defineTestCase('parse', TEST_STRING, {parseBooleans: true});
defineTestCase('parse', TEST_STRING, {sort: false});
defineTestCase('parse', TEST_STRING, {decode: false});
defineTestCase('parse', TEST_BRACKETS_STRING, {arrayFormat: 'bracket'});
defineTestCase('parse', TEST_INDEX_STRING, {arrayFormat: 'index'});
defineTestCase('parse', TEST_COMMA_STRING, {arrayFormat: 'comma'});
defineTestCase('parse', TEST_BRACKET_SEPARATOR_STRING, {arrayFormat: 'bracket-separator'});
// Stringify
defineTestCase('stringify', TEST_OBJECT);
defineTestCase('stringify', TEST_OBJECT, {strict: false});
defineTestCase('stringify', TEST_OBJECT, {encode: false});
defineTestCase('stringify', TEST_OBJECT, {skipNull: true});
defineTestCase('stringify', TEST_OBJECT, {skipEmptyString: true});
defineTestCase('stringify', TEST_OBJECT, {arrayFormat: 'bracket'});
defineTestCase('stringify', TEST_OBJECT, {arrayFormat: 'index'});
defineTestCase('stringify', TEST_OBJECT, {arrayFormat: 'comma'});
defineTestCase('stringify', TEST_OBJECT, {arrayFormat: 'bracket-separator'});
// Extract
defineTestCase('extract', TEST_URL);
// ParseUrl
defineTestCase('parseUrl', TEST_URL);
// StringifyUrl
defineTestCase('stringifyUrl', {url: TEST_HOST, query: TEST_OBJECT});
// Log/display the results
suite.on('cycle', event => {
const {name, hz} = event.target;
const opsPerSec = Math.round(hz).toLocaleString();
console.log(name.padEnd(46, '_') + opsPerSec.padStart(3, '_') + ' ops/s');
});
suite.run();