X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=random.js;h=8c0fa86e669b384e4d812478e00b6a61716031c8;hb=52d07fb863422041a24874efb893c711bf0b9cd4;hp=2f60a8c5f3c0354a6ad1f37de987824844840301;hpb=7fd91296ddf293e0ef084408618f4cc1f551de92;p=benchmarks-js.git diff --git a/random.js b/random.js index 2f60a8c..8c0fa86 100644 --- a/random.js +++ b/random.js @@ -1,18 +1,23 @@ -const Benchmark = require('benchmark') -const { LIST_FORMATTER, secureRandom } = require('./benchmark-utils') +const crypto = require('crypto') +const Benchmark = require('benny') +const { + secureRandom, + secureRandomWithRandomValues +} = require('./benchmark-utils') -const suite = new Benchmark.Suite() - -const maximum = 1000 +const maximum = 281474976710654 /** * @param max * @param min * @returns */ -function getSecureRandomInteger (max, min = 0) { +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 } @@ -24,30 +29,77 @@ function getSecureRandomInteger (max, min = 0) { * @param min * @returns */ -function getRandomInteger (max, min = 0) { +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) { + 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 != null && min !== 0) { min = Math.ceil(min) return Math.floor(Math.random() * (max - min + 1)) + min } return Math.floor(Math.random() * (max + 1)) } -suite - .add('Secure random integer generator', function () { +Benchmark.suite( + 'Random Integer Generator', + Benchmark.add('Secure random integer generator', () => { getSecureRandomInteger(maximum) - }) - .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: '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 }) - .on('cycle', function (event) { - console.log(event.target.toString()) - }) - .on('complete', function () { - console.log( - 'Fastest is ' + LIST_FORMATTER.format(this.filter('fastest').map('name')) - ) - // eslint-disable-next-line n/no-process-exit - process.exit() - }) - .run() +).catch(err => { + console.error(err) +})