Skip to content
Merged
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
29 changes: 22 additions & 7 deletions benchmark/bench-cmp-lib.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
'use strict'

const benchmark = require('benchmark')
const suite = new benchmark.Suite()
const { Bench } = require('tinybench')
const suite = new Bench({
name: 'Library Comparison Benchmarks',
time: 100,
setup: (_task, mode) => {
// Run the garbage collector before warmup at each cycle
if (mode === 'warmup' && typeof globalThis.gc === 'function') {
globalThis.gc()
}
}
})

const STR_LEN = 1e4
const LARGE_ARRAY_SIZE = 2e4
Expand Down Expand Up @@ -300,10 +309,16 @@ suite.add('compile-json-stringify date format', function () {
CJSStringifyDate(date)
})

suite.on('cycle', cycle)
suite.run().then(() => {
for (const task of suite.tasks) {
const hz = task.result.hz // ops/sec
const rme = task.result.rme // relative margin of error (%)
const samples = task.result.samples.length

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

function cycle (e) {
console.log(e.target.toString())
}
const output = `${task.name} x ${formattedHz} ops/sec ±${formattedRme}% (${samples} runs sampled)`
console.log(output)
}
}).catch(err => console.error(`Error: ${err.message}`))
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}`))
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"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",
"benchmark": "node --expose-gc ./benchmark/bench-cmp-lib.js",
"lint": "eslint",
"lint:fix": "eslint --fix",
"test:typescript": "tsd",
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