diff --git a/benchmark/bench-thread.js b/benchmark/bench-thread.js index 7e3ba73e..547c8d88 100644 --- a/benchmark/bench-thread.js +++ b/benchmark/bench-thread.js @@ -2,20 +2,33 @@ const { workerData: benchmark, parentPort } = require('worker_threads') -const Benchmark = require('benchmark') -Benchmark.options.minSamples = 100 +const { Bench } = require('tinybench') -const suite = Benchmark.Suite() +const bench = new Bench({ + name: benchmark.name, + time: 100, + setup: (_task, mode) => { + // Run the garbage collector before warmup at each cycle + if (mode === 'warmup' && typeof globalThis.gc === 'function') { + globalThis.gc() + } + } +}) const FJS = require('..') const stringify = FJS(benchmark.schema) -suite - .add(benchmark.name, () => { - stringify(benchmark.input) - }) - .on('cycle', (event) => { - parentPort.postMessage(String(event.target)) - }) - .on('complete', () => {}) - .run() +bench.add(benchmark.name, () => { + stringify(benchmark.input) +}).run().then(() => { + const task = bench.tasks[0] + const hz = task.result.hz // ops/sec + const rme = task.result.rme // relative margin of error (%) + const samples = task.result.samples.length + + const formattedHz = hz.toLocaleString('en-US', { maximumFractionDigits: 0 }) + const formattedRme = rme.toFixed(2) + + const output = `${task.name} x ${formattedHz} ops/sec ±${formattedRme}% (${samples} runs sampled)` + parentPort.postMessage(output) +}).catch(err => parentPort.postMessage(`Error: ${err.message}`)) diff --git a/package.json b/package.json index 7e0655bd..8014bd71 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "type": "commonjs", "types": "types/index.d.ts", "scripts": { - "bench": "node ./benchmark/bench.js", + "bench": "node --expose-gc ./benchmark/bench.js", "bench:cmp": "node ./benchmark/bench-cmp-branch.js", "bench:cmp:ci": "node ./benchmark/bench-cmp-branch.js --ci", "benchmark": "node ./benchmark/bench-cmp-lib.js", @@ -63,7 +63,6 @@ ], "devDependencies": { "@sinclair/typebox": "^0.34.3", - "benchmark": "^2.1.4", "c8": "^10.1.2", "cli-select": "^1.1.2", "compile-json-stringify": "^0.1.2", @@ -73,6 +72,7 @@ "json-accelerator": "^0.0.2", "neostandard": "^0.12.0", "simple-git": "^3.23.0", + "tinybench": "^5.0.1", "tsd": "^0.32.0", "webpack": "^5.90.3" },