Skip to content

Commit 7291942

Browse files
authored
feat: new benchmar with tinybench
Signed-off-by: francesco <[email protected]>
1 parent 2b37f1d commit 7291942

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

benchmark/bench-thread.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,33 @@
22

33
const { workerData: benchmark, parentPort } = require('worker_threads')
44

5-
const Benchmark = require('benchmark')
6-
Benchmark.options.minSamples = 100
5+
const { Bench } = require('tinybench')
76

8-
const suite = Benchmark.Suite()
7+
const bench = new Bench({
8+
name: benchmark.name,
9+
time: 100,
10+
setup: (_task, mode) => {
11+
// Run the garbage collector before warmup at each cycle
12+
if (mode === 'warmup' && typeof globalThis.gc === 'function') {
13+
globalThis.gc()
14+
}
15+
}
16+
})
917

1018
const FJS = require('..')
1119
const stringify = FJS(benchmark.schema)
1220

13-
suite
14-
.add(benchmark.name, () => {
15-
stringify(benchmark.input)
16-
})
17-
.on('cycle', (event) => {
18-
parentPort.postMessage(String(event.target))
19-
})
20-
.on('complete', () => {})
21-
.run()
21+
bench.add(benchmark.name, () => {
22+
stringify(benchmark.input)
23+
}).run().then(() => {
24+
const task = bench.tasks[0]
25+
const hz = task.result.hz // ops/sec
26+
const rme = task.result.rme // relative margin of error (%)
27+
const samples = task.result.samples.length
28+
29+
const formattedHz = hz.toLocaleString('en-US', { maximumFractionDigits: 0 })
30+
const formattedRme = rme.toFixed(2)
31+
32+
const output = `${task.name} x ${formattedHz} ops/sec ±${formattedRme}% (${samples} runs sampled)`
33+
parentPort.postMessage(output)
34+
}).catch(err => parentPort.postMessage(`Error: ${err.message}`))

0 commit comments

Comments
 (0)