-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
benchmark.js
95 lines (74 loc) · 1.87 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
87
88
89
90
91
92
93
94
95
'use strict'
const benchmark = require('benchmark')
const Hashids = require('hashids')
const shortid = require('shortid')
const nid = require('nid')
const crypto = require('crypto')
const uuid = require('uuid')
const { nanoid } = require('nanoid')
const hyperid = require('.')
const napiRsUuid = require('@napi-rs/uuid').v4
const hyperIdSafeUrlInstance = hyperid({
urlSafe: true
})
const hyperIdMaxIntInstance = hyperid({
maxInt: 10000 // lesser the maxInt, faster the performance.
})
const hyperIdInstance = hyperid()
const suite = new benchmark.Suite()
const hashids = new Hashids('mybench')
if (crypto.randomUUID) {
suite.add('crypto.randomUUID', function () {
crypto.randomUUID()
})
}
suite.add('hashids process.hrtime', function () {
hashids.encode(process.hrtime())
})
let hashIdCounter = 0
suite.add('hashids counter', function () {
hashids.encode(hashIdCounter++)
})
suite.add('shortid', function () {
shortid()
})
suite.add('crypto.random', function () {
crypto.randomBytes(33).toString('hex')
})
suite.add('nid', function () {
nid()
})
suite.add('uuid.v4', function () {
uuid.v4()
})
suite.add('napiRsUuid.v4', function () {
napiRsUuid()
})
suite.add('uuid.v1', function () {
uuid.v1()
})
suite.add('nanoid', function () {
nanoid()
})
suite.add('hyperid - variable length', function () {
hyperIdInstance()
})
suite.add('hyperid - fixed length', function () {
hyperIdInstance(true)
})
suite.add('hyperid - fixed length, url safe', function () {
hyperIdSafeUrlInstance(true)
})
suite.add('hyperid - max int', function () {
hyperIdMaxIntInstance()
})
suite.on('cycle', cycle)
suite.on('complete', function () {
console.log('\n')
console.log(`Fastest is ${this.filter('fastest').map('name')}`)
console.log(`Slowest is ${this.filter('slowest').map('name')}`)
})
suite.run({ async: true })
function cycle (e) {
console.log(e.target.toString())
}