Bump nanoid from 3.1.25 to 3.2.0
[benchmarks-js.git] / random.js
1 const Benchmark = require('benchmark')
2 const { LIST_FORMATTER, secureRandom } = require('./benchmark-utils')
3
4 const suite = new Benchmark.Suite()
5
6 const maximum = 1000
7
8 /**
9 * @param max
10 * @param min
11 */
12 function getSecureRandomInteger (max, min = 0) {
13 max = Math.floor(max)
14 if (min) {
15 min = Math.ceil(min)
16 return Math.floor(secureRandom() * (max - min + 1)) + min
17 }
18 return Math.floor(secureRandom() * (max + 1))
19 }
20
21 /**
22 * @param max
23 * @param min
24 */
25 function getRandomInteger (max, min = 0) {
26 max = Math.floor(max)
27 if (min) {
28 min = Math.ceil(min)
29 return Math.floor(Math.random() * (max - min + 1)) + min
30 }
31 return Math.floor(Math.random() * (max + 1))
32 }
33
34 suite
35 .add('Secure random integer generator', function () {
36 getSecureRandomInteger(maximum)
37 })
38 .add('Random integer generator', function () {
39 getRandomInteger(maximum)
40 })
41 .on('cycle', function (event) {
42 console.log(event.target.toString())
43 })
44 .on('complete', function () {
45 console.log(
46 'Fastest is ' + LIST_FORMATTER.format(this.filter('fastest').map('name'))
47 )
48 // eslint-disable-next-line no-process-exit
49 process.exit()
50 })
51 .run()