import { randomInt } from 'node:crypto'
+
import Benchmark from 'benny'
+
import {
secureRandom,
secureRandomWithRandomValues
} from './benchmark-utils.mjs'
-const maximum = 281474976710654
+const maximum = 281474976710655
/**
* @param max
throw new RangeError('Invalid interval')
}
max = Math.floor(max)
- if (min != null && min !== 0) {
+ if (min !== 0) {
min = Math.ceil(min)
return Math.floor(secureRandom() * (max - min + 1)) + min
}
throw new RangeError('Invalid interval')
}
max = Math.floor(max)
- if (min != null && min !== 0) {
+ if (min !== 0) {
min = Math.ceil(min)
return Math.floor(secureRandomWithRandomValues() * (max - min + 1)) + min
}
throw new RangeError('Invalid interval')
}
max = Math.floor(max)
- if (min != null && min !== 0) {
+ if (min !== 0) {
min = Math.ceil(min)
return Math.floor(Math.random() * (max - min + 1)) + min
}
getSecureRandomIntegerWithRandomValues(maximum)
}
),
- Benchmark.add('Crypto random integer generator', (max = maximum, min = 0) => {
- max = Math.floor(max)
- if (min != null && min !== 0) {
- min = Math.ceil(min)
- return Math.floor(randomInt(min, max + 1))
- }
- return Math.floor(randomInt(max + 1))
+ Benchmark.add('Crypto random integer generator', () => {
+ randomInt(maximum)
}),
Benchmark.add('Math random integer generator', () => {
getRandomInteger(maximum)