Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions benchmark/bench-thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`))
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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"
},
Expand Down