X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=random.js;h=ee5cc617ed12a80050a62cb65a707bd8b2da4899;hb=47006ddf6055b02dd084ae91269f12b44ce8225d;hp=4b41596fd2b0ad3d1a6adaa818ec941135b48644;hpb=aa697b2fb25ad4f2504dfc2a67ecd03d4e58fc77;p=benchmarks-js.git diff --git a/random.js b/random.js index 4b41596..ee5cc61 100644 --- a/random.js +++ b/random.js @@ -1,25 +1,16 @@ -const Benchmark = require('benchmark') -const crypto = require('crypto') -const { LIST_FORMATTER } = require('./benchmark-utils') +const Benchmark = require('benny') +const { secureRandom } = require('./benchmark-utils') -const suite = new Benchmark.Suite() - -const maximum = 1000 - -/** - * - */ -function secureRandom () { - return crypto.randomBytes(4).readUInt32LE() / 0x100000000 -} +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) { max = Math.floor(max) - if (min) { + if (min != null && min !== 0) { min = Math.ceil(min) return Math.floor(secureRandom() * (max - min + 1)) + min } @@ -29,31 +20,40 @@ function getSecureRandomInteger (max, min = 0) { /** * @param max * @param min + * @returns */ -function getRandomInteger (max, min = 0) { +function getRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { 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() +)