Skip to content

Commit

Permalink
Replace benchmark with tinybench (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonkoops authored Dec 23, 2023
1 parent ca4089f commit 77d5799
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 186 deletions.
72 changes: 72 additions & 0 deletions benchmarks/benchmarks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Bench } from 'tinybench';

import local from 'classnames-local';
import dedupe from 'classnames-local/dedupe.js';
import localPackage from 'classnames-local/package.json' with { type: 'json' };

import npm from 'classnames-npm';
import npmDedupe from 'classnames-npm/dedupe.js';
import npmPackage from 'classnames-npm/package.json' with { type: 'json' };

if (localPackage.version !== npmPackage.version) {
console.warn(
`Your local version (${localPackage.version}) does not match the installed version (${npmPackage.version}).\n\n` +
'Please run `npm update classnames-npm` in ./benchmarks to ensure you are benchmarking against the latest version published to NPM.\n'
);
}

const benchmarks = [
{
description: 'strings',
args: ['one', 'two', 'three']
},
{
description: 'object',
args: [{one: true, two: true, three: false}]
},
{
description: 'strings, object',
args: ['one', 'two', {four: true, three: false}]
},
{
description: 'mix',
args: ['one', {two: true, three: false}, {four: 'four', five: true}, 6, {}]
},
{
description: 'arrays',
args: [['one', 'two'], ['three'], ['four', ['five']], [{six: true}, {seven: false}]]
}
];

export async function runBenchmarks () {
for (const benchmark of benchmarks) {
console.log(`Benchmarking '${benchmark.description}'.`);
const bench = await runBenchmark(benchmark);
console.table(bench.table());
}

console.log('Finished!');
}

async function runBenchmark (benchmark) {
const bench = new Bench();

bench.add(`local#${benchmark.description}`, () => {
local(...benchmark.args);
});

bench.add(`npm#${benchmark.description}`, () => {
npm(...benchmark.args);
});

bench.add(`local/dedupe#${benchmark.description}`, () => {
dedupe(...benchmark.args);
});

bench.add(`npm/dedupe#${benchmark.description}`, () => {
npmDedupe(...benchmark.args);
});

await bench.run();
return bench;
}
29 changes: 0 additions & 29 deletions benchmarks/fixtures.js

This file was deleted.

3 changes: 1 addition & 2 deletions benchmarks/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
<title>Benchmarks</title>
</head>
<body>
<button id="start" type="button" disabled>Start benchmarks</button>
<pre id="results"></pre>
<button id="start" type="button">Start benchmarks</button>
<script type="module" src="./runInBrowser.bundle.js"></script>
</body>
</html>
7 changes: 3 additions & 4 deletions benchmarks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
"type": "module",
"scripts": {
"benchmarks": "node ./run.js",
"benchmarks-browser": "rollup --plugin commonjs,json,node-resolve ./runInBrowser.js --file ./runInBrowser.bundle.js && http-server -o -c-1"
"benchmarks-browser": "rollup --plugin commonjs,json,node-resolve ./runInBrowser.js --file ./runInBrowser.bundle.js && http-server -c-1"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"benchmark": "^2.1.4",
"classnames-local": "file:../",
"classnames-npm": "npm:classnames@*",
"http-server": "^14.1.1",
"lodash": "^4.17.21",
"rollup": "^4.9.1"
"rollup": "^4.9.1",
"tinybench": "^2.5.1"
}
}
30 changes: 2 additions & 28 deletions benchmarks/run.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,3 @@
import local from 'classnames-local';
import dedupe from 'classnames-local/dedupe.js';
import localPackage from 'classnames-local/package.json' with { type: 'json' };
import { runBenchmarks } from './benchmarks.js';

import npm from 'classnames-npm';
import npmDedupe from 'classnames-npm/dedupe.js';
import npmPackage from 'classnames-npm/package.json' with { type: 'json' };

import fixtures from './fixtures.js';
import runChecks from './runChecks.js';
import runSuite from './runSuite.js';

if (localPackage.version !== npmPackage.version) {
log(
`Your local version (${localPackage.version} does not match the installed version (${npmPackage.version})\n\n` +
'Please run `npm update classnames-npm` in ./benchmarks to ensure you are benchmarking\n' +
'the latest version of this package.\n'
);
process.exit(0);
}

fixtures.forEach((f) => {
runChecks(local, npm, dedupe, npmDedupe, f);
runSuite(local, npm, dedupe, npmDedupe, f, log);
});

function log (message) {
console.log(message);
}
await runBenchmarks();
15 changes: 0 additions & 15 deletions benchmarks/runChecks.js

This file was deleted.

53 changes: 5 additions & 48 deletions benchmarks/runInBrowser.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,9 @@
import local from 'classnames-local';
import dedupe from 'classnames-local/dedupe.js';
import localPackage from 'classnames-local/package.json' with { type: 'json' };

import npm from 'classnames-npm';
import npmDedupe from 'classnames-npm/dedupe.js';
import npmPackage from 'classnames-npm/package.json' with { type: 'json' };

import fixtures from './fixtures.js';
import runSuite from './runSuite.js';
import { runBenchmarks } from './benchmarks.js';

const startButton = document.getElementById('start');
const results = document.getElementById('results');

startButton.addEventListener('click', runBenchmarks);

if (localPackage.version === npmPackage.version) {
startButton.addEventListener('click', async () => {
startButton.disabled = true;
await runBenchmarks();
startButton.disabled = false;
} else {
startButton.style.display = 'none';

log(
`Your local version (${localPackage.version} does not match the installed version (${npmPackage.version})\n\n` +
'Please run `npm update classnames-npm` in ./benchmarks to ensure you are benchmarking\n' +
'the latest version of this package.\n'
);
}

async function runBenchmarks () {
startButton.style.display = 'none';

log('Running benchmark…');
log(navigator.userAgent);

await nextTick();

for (const fixture of fixtures) {
runSuite(local, npm, dedupe, npmDedupe, fixture, log);
await nextTick();
}

log('Finished!');
}

function log (message) {
console.log(message);
results.textContent += `${message}\n`;
}

function nextTick () {
return new Promise((resolve) => setTimeout(resolve));
}
});
41 changes: 0 additions & 41 deletions benchmarks/runSuite.js

This file was deleted.

27 changes: 8 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 77d5799

Please sign in to comment.