X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=random.js;h=f4fd9f0bd53f9182f13541be0b3bb17bc7c5e27c;hb=43a6273cfba404cda3b7e809f4f3074af3808f68;hp=98b59ed34f5241db671aad996fecc51b58ecdb05;hpb=d6c407132906074921b9274f3473b148c947dffc;p=benchmarks-js.git diff --git a/random.js b/random.js index 98b59ed..f4fd9f0 100644 --- a/random.js +++ b/random.js @@ -1,17 +1,19 @@ -const Benchmark = require('benchmark') -const { LIST_FORMATTER, secureRandom } = require('./benchmark-utils') +const Benchmark = require('benny') +const { secureRandom } = require('./benchmark-utils') -const suite = new Benchmark.Suite() - -const maximum = 1000 +const maximum = Number.MAX_SAFE_INTEGER /** * @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 } @@ -21,31 +23,43 @@ function getSecureRandomInteger (max, min = 0) { /** * @param max * @param min + * @returns */ -function getRandomInteger (max, min = 0) { +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 } 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('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 no-process-exit - process.exit() - }) - .run() +)