random generator benchmark
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded
This commit is contained in:
parent
8a49e38285
commit
598548d97b
1 changed files with 46 additions and 0 deletions
46
benchmark/random.js
Normal file
46
benchmark/random.js
Normal file
|
@ -0,0 +1,46 @@
|
|||
import crypto from 'crypto'
|
||||
import Benchmark from 'benchmarkjs-pretty'
|
||||
|
||||
function TestGenerateRandomString() {
|
||||
|
||||
return new Benchmark.default('test different method to generate random string)')
|
||||
.add('crypto.randomBytes(16)', function() {
|
||||
for (let i = 0; i < 25; i++) {
|
||||
crypto.randomBytes(16).toString('base64')
|
||||
}
|
||||
})
|
||||
.add('crypto.randomBytes(32)', function() {
|
||||
for (let i = 0; i < 25; i++) {
|
||||
crypto.randomBytes(32).toString('base64')
|
||||
}
|
||||
})
|
||||
.add('random (11 characters long)', function() {
|
||||
for (let i = 0; i < 25; i++) {
|
||||
let out = Math.random().toString(36).substring(2, 14)
|
||||
}
|
||||
})
|
||||
.add('random (22 characters long)', function() {
|
||||
for (let i = 0; i < 25; i++) {
|
||||
let out = Math.random().toString(36).substring(2, 24)
|
||||
+ Math.random().toString(36).substring(2, 24)
|
||||
}
|
||||
})
|
||||
.add('random (44 characters long)', function() {
|
||||
for (let i = 0; i < 25; i++) {
|
||||
let out = Math.random().toString(36).substring(2, 24)
|
||||
+ Math.random().toString(36).substring(2, 24)
|
||||
+ Math.random().toString(36).substring(2, 24)
|
||||
+ Math.random().toString(36).substring(2, 24)
|
||||
}
|
||||
})
|
||||
.run()
|
||||
.then(function() {}, function(e) {
|
||||
console.error('error:', e)
|
||||
process.exit(1)
|
||||
})
|
||||
}
|
||||
|
||||
TestGenerateRandomString()
|
||||
.then(function() {
|
||||
process.exit(0)
|
||||
})
|
Loading…
Reference in a new issue