Skip to content

Commit

Permalink
change benchmark to the tinybench (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
GusevP authored Jun 25, 2024
1 parent 2b18fea commit 235d347
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
"@size-limit/file": "^11.1.4",
"@size-limit/webpack": "^11.1.4",
"@types/node": "^20.14.6",
"benchmark": "^2.1.4",
"better-node-test": "^0.5.1",
"c8": "^10.1.2",
"clean-publish": "^5.0.0",
Expand All @@ -65,6 +64,7 @@
"shortid": "^2.2.16",
"size-limit": "^11.1.4",
"terser": "^5.31.1",
"tinybench": "^2.8.0",
"uid": "^2.0.2",
"uid-safe": "^2.1.5",
"uuid": "^10.0.0",
Expand Down
29 changes: 8 additions & 21 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 17 additions & 14 deletions test/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import { v4 as lukeed4 } from '@lukeed/uuid'
import { v4 as napiV4 } from '@napi-rs/uuid'
import benchmark from 'benchmark'
import crypto from 'node:crypto'
import { styleText } from 'node:util'
import rndm from 'rndm'
import srs from 'secure-random-string'
import shortid from 'shortid'
import { Bench } from 'tinybench'
import { uid } from 'uid'
import uidSafe from 'uid-safe'
import { uid as uidSecure } from 'uid/secure'
Expand All @@ -17,7 +17,7 @@ import { nanoid as browser } from '../index.browser.js'
import { customAlphabet, nanoid } from '../index.js'
import { nanoid as nonSecure } from '../non-secure/index.js'

let suite = new benchmark.Suite()
let bench = new Bench()

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

Expand All @@ -27,7 +27,7 @@ function formatNumber(number) {
.replace(/^(\d|\d\d)(\d{3},)/, '$1,$2')
}

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

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 235d347

Please sign in to comment.