X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=random.js;h=8c0fa86e669b384e4d812478e00b6a61716031c8;hb=77d3850e9eb4f27dfa31bebc8e02f400848517ad;hp=72b0b919e27b818d339b4f14d8c114bba40b534d;hpb=f10b1da0dc4d21b8b8b8ba91283809028dd710e9;p=benchmarks-js.git diff --git a/random.js b/random.js index 72b0b91..8c0fa86 100644 --- a/random.js +++ b/random.js @@ -1,7 +1,11 @@ +const crypto = require('crypto') const Benchmark = require('benny') -const { secureRandom } = require('./benchmark-utils') +const { + secureRandom, + secureRandomWithRandomValues +} = require('./benchmark-utils') -const maximum = Number.MAX_SAFE_INTEGER +const maximum = 281474976710654 /** * @param max @@ -9,22 +13,48 @@ const maximum = Number.MAX_SAFE_INTEGER * @returns */ function getSecureRandomInteger (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) { + 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 + * @param min + * @returns + */ +function getSecureRandomIntegerWithRandomValues ( + 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(secureRandomWithRandomValues() * (max - min + 1)) + min + } + return Math.floor(secureRandomWithRandomValues() * (max + 1)) +} + /** * @param max * @param min * @returns */ function getRandomInteger (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) { + if (min != null && min !== 0) { min = Math.ceil(min) return Math.floor(Math.random() * (max - min + 1)) + min } @@ -33,14 +63,43 @@ function getRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { Benchmark.suite( 'Random Integer Generator', - Benchmark.add('Secure random integer generator', function () { + Benchmark.add('Secure random integer generator', () => { getSecureRandomInteger(maximum) }), - Benchmark.add('Random integer generator', function () { + Benchmark.add( + 'Secure random with getRandomValues() integer generator', + () => { + 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('Math random integer generator', () => { getRandomInteger(maximum) }), Benchmark.cycle(), Benchmark.complete(), - Benchmark.save({ file: 'random-integer-generator', format: 'chart.html' }), - Benchmark.save({ file: 'random-integer-generator', format: 'table.html' }) -) + Benchmark.save({ + file: 'random-integer-generator', + format: 'json', + details: true + }), + Benchmark.save({ + file: 'random-integer-generator', + format: 'chart.html', + details: true + }), + Benchmark.save({ + file: 'random-integer-generator', + format: 'table.html', + details: true + }) +).catch(err => { + console.error(err) +})