1 const Benchmark
= require('benchmark')
2 const crypto
= require('crypto')
3 const { LIST_FORMATTER
} = require('./benchmark-utils')
5 const suite
= new Benchmark
.Suite()
12 function secureRandom () {
13 return crypto
.randomBytes(4).readUInt32LE() / 0x100000000
20 function getSecureRandomInteger (max
, min
= 0) {
24 return Math
.floor(secureRandom() * (max
- min
+ 1)) + min
26 return Math
.floor(secureRandom() * (max
+ 1))
33 function getRandomInteger (max
, min
= 0) {
37 return Math
.floor(Math
.random() * (max
- min
+ 1)) + min
39 return Math
.floor(Math
.random() * (max
+ 1))
43 .add('Secure random integer generator', function () {
44 getSecureRandomInteger(maximum
)
46 .add('Random integer generator', function () {
47 getRandomInteger(maximum
)
49 .on('cycle', function (event
) {
50 console
.log(event
.target
.toString())
52 .on('complete', function () {
54 'Fastest is ' + LIST_FORMATTER
.format(this.filter('fastest').map('name'))
56 // eslint-disable-next-line no-process-exit