Skip to content

Commit d02e795

Browse files
authored
feat: tinybench also for library comparison benchmarks
1 parent 73c07c7 commit d02e795

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

‎benchmark/bench-cmp-lib.js‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
'use strict'
22

3-
const benchmark = require('benchmark')
4-
const suite = new benchmark.Suite()
3+
const { Bench } = require('tinybench')
4+
const suite = new Bench({
5+
name: 'Library Comparison Benchmarks',
6+
time: 100,
7+
setup: (_task, mode) => {
8+
// Run the garbage collector before warmup at each cycle
9+
if (mode === 'warmup' && typeof globalThis.gc === 'function') {
10+
globalThis.gc()
11+
}
12+
}
13+
})
514

615
const STR_LEN = 1e4
716
const LARGE_ARRAY_SIZE = 2e4
@@ -300,10 +309,16 @@ suite.add('compile-json-stringify date format', function () {
300309
CJSStringifyDate(date)
301310
})
302311

303-
suite.on('cycle', cycle)
312+
suite.run().then(() => {
313+
for (const task of suite.tasks) {
314+
const hz = task.result.hz // ops/sec
315+
const rme = task.result.rme // relative margin of error (%)
316+
const samples = task.result.samples.length
304317

305-
suite.run()
318+
const formattedHz = hz.toLocaleString('en-US', { maximumFractionDigits: 0 })
319+
const formattedRme = rme.toFixed(2)
306320

307-
function cycle (e) {
308-
console.log(e.target.toString())
309-
}
321+
const output = `${task.name} x ${formattedHz} ops/sec ±${formattedRme}% (${samples} runs sampled)`
322+
console.log(output)
323+
}
324+
}).catch(err => console.error(`Error: ${err.message}`))

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"bench": "node --expose-gc ./benchmark/bench.js",
1010
"bench:cmp": "node ./benchmark/bench-cmp-branch.js",
1111
"bench:cmp:ci": "node ./benchmark/bench-cmp-branch.js --ci",
12-
"benchmark": "node ./benchmark/bench-cmp-lib.js",
12+
"benchmark": "node --expose-gc ./benchmark/bench-cmp-lib.js",
1313
"lint": "eslint",
1414
"lint:fix": "eslint --fix",
1515
"test:typescript": "tsd",

0 commit comments

Comments
 (0)