Use benny in some benchmarks
[benchmarks-js.git] / benchmark-utils.js
... / ...
CommitLineData
1const crypto = require('crypto')
2
3/**
4 * Generate a cryptographically secure random number in the [0,1[ range
5 *
6 * @returns
7 */
8function secureRandom () {
9 return crypto.randomBytes(4).readUInt32LE() / 0x100000000
10}
11
12/**
13 * @param max
14 * @param min
15 * @returns
16 */
17function generateRandomInteger (max, min = 0) {
18 if (max < 0) {
19 throw new RangeError('Invalid interval')
20 }
21 max = Math.floor(max)
22 if (min) {
23 if (max < min || min < 0) {
24 throw new RangeError('Invalid interval')
25 }
26 min = Math.ceil(min)
27 return Math.floor(secureRandom() * (max - min + 1)) + min
28 }
29 return Math.floor(secureRandom() * (max + 1))
30}
31
32/**
33 * @param ms
34 * @returns
35 */
36async function sleep (ms) {
37 return new Promise(resolve => setTimeout(resolve, ms))
38}
39
40const LIST_FORMATTER = new Intl.ListFormat('en-US', {
41 style: 'long',
42 type: 'conjunction'
43})
44
45module.exports = { generateRandomInteger, sleep, secureRandom, LIST_FORMATTER }