X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=benchmark-utils.mjs;h=d0d5a62291527bc12387e18188991e83e307670a;hb=HEAD;hp=7c7832e897d76061383214a3763142fa68159c6a;hpb=b77517bab6445c121ec357749683416b4c656438;p=benchmarks-js.git diff --git a/benchmark-utils.mjs b/benchmark-utils.mjs index 7c7832e..8e32f90 100644 --- a/benchmark-utils.mjs +++ b/benchmark-utils.mjs @@ -1,4 +1,4 @@ -import { randomBytes, getRandomValues } from 'node:crypto' +import { getRandomValues, randomBytes, randomInt } from 'node:crypto' /** * Generate a cryptographically secure random number in the [0,1[ range @@ -16,23 +16,6 @@ export function secureRandomWithRandomValues () { return getRandomValues(new Uint32Array(1))[0] / 0x100000000 } -/** - * @param max - * @param min - * @returns - */ -export function generateRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { - if (max < min || max < 0 || min < 0) { - throw new RangeError('Invalid interval') - } - max = Math.floor(max) - if (min != null && min !== 0) { - min = Math.ceil(min) - return Math.floor(secureRandom() * (max - min + 1)) + min - } - return Math.floor(secureRandom() * (max + 1)) -} - /** * * @param max @@ -43,10 +26,10 @@ export function generateRandomFloat (max = Number.MAX_VALUE, min = 0) { if (max < min) { throw new RangeError('Invalid interval') } - if (max - min === Infinity) { + if (max - min === Number.POSITIVE_INFINITY) { throw new RangeError('Invalid interval') } - return (crypto.randomBytes(4).readUInt32LE() / 0xffffffff) * (max - min) + min + return (randomBytes(4).readUInt32LE() / 0xffffffff) * (max - min) + min } /** @@ -80,7 +63,7 @@ export function generateRandomObject ( numberMax = Number.MAX_VALUE, numberGenerator = generateRandomFloat ) { - const size = generateRandomInteger(sizeMax) + const size = randomInt(sizeMax) const object = {} for (let i = 0; i < size; i++) { object[i.toString()] = numberGenerator(numberMax)