random generator benchmark
continuous-integration/appveyor/branch AppVeyor build succeeded Details

master
Jonatan Nilsson 2023-10-07 11:49:41 +00:00
parent 8a49e38285
commit 598548d97b
1 changed files with 46 additions and 0 deletions

46
benchmark/random.js Normal file
View 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)
})