Skip to content

Commit

Permalink
change benchmark to the tinybench (ai#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
GusevP authored and yhz committed Jun 25, 2024
1 parent 5b3b396 commit 4fac1c2
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions test/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ import { nanoid as browser } from '../index.browser.js'
import { customAlphabet, nanoid } from '../index.js'
import { nanoid as nonSecure } from '../non-secure/index.js'

const bench = new Bench()
const nanoid2 = customAlphabet('1234567890abcdef-', 10)
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', () => {
Expand Down Expand Up @@ -64,25 +71,16 @@ bench
rndm(21)
})

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

let name = task.name.padEnd(longestNameLength)
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`)
})

return Math.max(maxTaskNameLength, task.name.length)
}, 0)
bench.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`
)
})

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

0 comments on commit 4fac1c2

Please sign in to comment.