Skip to content

Commit

Permalink
Add benchmarks (#254)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
Vlad Shilov and sindresorhus committed Apr 27, 2020
1 parent 1e70bf3 commit 5d19c56
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
72 changes: 72 additions & 0 deletions benchmark.js
@@ -0,0 +1,72 @@
'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_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'});

// 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'});

// 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(36, '_') + opsPerSec.padStart(12, '_') + ' ops/s');
});

suite.run();
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -14,6 +14,7 @@
"node": ">=6"
},
"scripts": {
"benchmark": "node benchmark.js",
"test": "xo && ava && tsd"
},
"files": [
Expand All @@ -36,12 +37,13 @@
"searchparams"
],
"dependencies": {
"split-on-first": "^1.0.0",
"decode-uri-component": "^0.2.0",
"split-on-first": "^1.0.0",
"strict-uri-encode": "^2.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"benchmark": "^2.1.4",
"deep-equal": "^1.0.1",
"fast-check": "^1.5.0",
"tsd": "^0.7.3",
Expand Down

0 comments on commit 5d19c56

Please sign in to comment.