|
2 | 2 |
|
3 | 3 | const { workerData: benchmark, parentPort } = require('worker_threads') |
4 | 4 |
|
5 | | -const Benchmark = require('benchmark') |
6 | | -Benchmark.options.minSamples = 100 |
| 5 | +const { Bench } = require('tinybench') |
7 | 6 |
|
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 | +}) |
9 | 17 |
|
10 | 18 | const FJS = require('..') |
11 | 19 | const stringify = FJS(benchmark.schema) |
12 | 20 |
|
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