Skip to content

Commit

Permalink
Benchmark refactor (#488)
Browse files Browse the repository at this point in the history
* Refactor benchmark cycle event listener

* Changes to support code style
  • Loading branch information
yhz authored Jun 25, 2024
1 parent 69995ab commit 4bc4dd0
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions test/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ let bench = new Bench()

let nanoid2 = customAlphabet('1234567890abcdef-', 10)

function formatNumber(number) {
return String(number)
.replace(/\d{3}$/, ',$&')
.replace(/^(\d|\d\d)(\d{3},)/, '$1,$2')
}

bench
.add('crypto.randomUUID', () => {
crypto.randomUUID()
Expand Down Expand Up @@ -70,16 +64,26 @@ bench
.add('rndm', () => {
rndm(21)
})
.addEventListener('cycle', event => {
let name = event.task.name.padEnd('async secure-random-string'.length)
let hz = formatNumber(event.task.result.hz.toFixed(0)).padStart(10)
if (event.task.name === 'uid') {
name = '\nNon-secure:\n' + name
}
process.stdout.write(
`${name}${styleText('bold', hz)}${styleText('dim', ' ops/sec')}\n`
)
})

let longestTask = bench.tasks.reduce(
(maxLength, task) => Math.max(maxLength, task.name.length), 0
)

bench.addEventListener('cycle', ({ task }) => {
let hz = (+task.result.hz.toFixed(0))
.toLocaleString('en-US')
.padStart(14)

let name = task.name.padEnd(longestTask)
let value = styleText('bold', hz)
let units = styleText('dim', 'ops/sec')

if (task.name === 'uid') {
process.stdout.write('\nNon-secure:\n')
}

process.stdout.write(`${name}${value} ${units}\n`)
})

await bench.warmup()
await bench.run()

0 comments on commit 4bc4dd0

Please sign in to comment.