-
-
Notifications
You must be signed in to change notification settings - Fork 806
/
benchmark.js
executable file
·86 lines (75 loc) · 1.9 KB
/
benchmark.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env node
import { v4 as lukeed4 } from '@lukeed/uuid'
import { v4 as napiV4 } from '@napi-rs/uuid'
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'
import { v4 as uuid4 } from 'uuid'
import { nanoid as browser } from '../index.browser.js'
import { customAlphabet, nanoid } from '../index.js'
import { nanoid as nonSecure } from '../non-secure/index.js'
let bench = new Bench()
let nanoid2 = customAlphabet('1234567890abcdef-', 10)
bench
.add('crypto.randomUUID', () => {
crypto.randomUUID()
})
.add('uuid v4', () => {
uuid4()
})
.add('@napi-rs/uuid', () => {
napiV4()
})
.add('uid/secure', () => {
uidSecure(32)
})
.add('@lukeed/uuid', () => {
lukeed4()
})
.add('nanoid', () => {
nanoid()
})
.add('customAlphabet', () => {
nanoid2()
})
.add('nanoid for browser', () => {
browser()
})
.add('secure-random-string', () => {
srs()
})
.add('uid-safe.sync', () => {
uidSafe.sync(14)
})
.add('shortid', () => {
shortid()
})
.add('uid', () => {
uid(32)
})
.add('nanoid/non-secure', () => {
nonSecure()
})
.add('rndm', () => {
rndm(21)
})
let longestTask = bench.tasks.reduce((maxLength, task) => {
return 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.run({ warmup: true })