repositories
/
benchmarks-js.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Merge pull request #1 from jerome-benoit/dependabot/npm_and_yarn/nanoid-3.2.0
[benchmarks-js.git]
/
benchmark-utils.js
diff --git
a/benchmark-utils.js
b/benchmark-utils.js
index c1b68c581615b842ec7804111f71aafbf7f8c279..841cef87a6a9bc32bac9fa2bd161b76870050e3f 100644
(file)
--- a/
benchmark-utils.js
+++ b/
benchmark-utils.js
@@
-1,12
+1,38
@@
+const crypto = require('crypto')
+
+/**
+ * Generate a cryptographically secure random number in the [0,1[ range
+ *
+ * @returns
+ */
+function secureRandom () {
+ return crypto.randomBytes(4).readUInt32LE() / 0x100000000
+}
+
/**
* @param max
* @param min
*/
function generateRandomInteger (max, min = 0) {
/**
* @param max
* @param min
*/
function generateRandomInteger (max, min = 0) {
+ if (max < 0) {
+ throw new RangeError('Invalid interval')
+ }
+ max = Math.floor(max)
if (min) {
if (min) {
- return Math.floor(Math.random() * (max - min + 1) + min)
+ if (max < min || min < 0) {
+ throw new RangeError('Invalid interval')
+ }
+ min = Math.ceil(min)
+ return Math.floor(secureRandom() * (max - min + 1)) + min
}
}
- return Math.floor(Math.random() * max + 1)
+ return Math.floor(secureRandom() * (max + 1))
+}
+
+/**
+ * @param ms
+ */
+async function sleep (ms) {
+ return new Promise(resolve => setTimeout(resolve, ms))
}
const LIST_FORMATTER = new Intl.ListFormat('en-US', {
}
const LIST_FORMATTER = new Intl.ListFormat('en-US', {
@@
-14,4
+40,4
@@
const LIST_FORMATTER = new Intl.ListFormat('en-US', {
type: 'conjunction'
})
type: 'conjunction'
})
-module.exports = { generateRandomInteger, LIST_FORMATTER }
+module.exports = { generateRandomInteger,
sleep, secureRandom,
LIST_FORMATTER }