build(deps-dev): apply updates
[benchmarks-js.git] / random.mjs
index d59c81359809a995111ba10bab27ef4874a8bd8b..98ff2a22e36d98c4a0b0a1cf018302e5b0bbbb7d 100644 (file)
@@ -1,11 +1,13 @@
-import crypto from 'crypto'
+import { randomInt } from 'node:crypto'
+
 import Benchmark from 'benny'
+
 import {
   secureRandom,
   secureRandomWithRandomValues
 } from './benchmark-utils.mjs'
 
-const maximum = 281474976710654
+const maximum = 281474976710655
 
 /**
  * @param max
@@ -17,7 +19,7 @@ function getSecureRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) {
     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
   }
@@ -37,7 +39,7 @@ function getSecureRandomIntegerWithRandomValues (
     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
   }
@@ -54,7 +56,7 @@ function getRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) {
     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
   }
@@ -72,13 +74,8 @@ Benchmark.suite(
       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(crypto.randomInt(min, max + 1))
-    }
-    return Math.floor(crypto.randomInt(max + 1))
+  Benchmark.add('Crypto random integer generator', () => {
+    randomInt(maximum)
   }),
   Benchmark.add('Math random integer generator', () => {
     getRandomInteger(maximum)
@@ -100,6 +97,4 @@ Benchmark.suite(
     format: 'table.html',
     details: true
   })
-).catch((err) => {
-  console.error(err)
-})
+).catch(console.error)