From: Jérôme Benoit Date: Sun, 23 Oct 2022 21:20:10 +0000 (+0200) Subject: Use benny in random integer generator benchmark X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=f10b1da0dc4d21b8b8b8ba91283809028dd710e9;p=benchmarks-js.git Use benny in random integer generator benchmark Signed-off-by: Jérôme Benoit --- diff --git a/benchmark-utils.js b/benchmark-utils.js index 1e7914f..164cfc6 100644 --- a/benchmark-utils.js +++ b/benchmark-utils.js @@ -14,7 +14,7 @@ function secureRandom () { * @param min * @returns */ -function generateRandomInteger (max, min = 0) { +function generateRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { if (max < 0) { throw new RangeError('Invalid interval') } diff --git a/random.js b/random.js index 2f60a8c..72b0b91 100644 --- a/random.js +++ b/random.js @@ -1,16 +1,14 @@ -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) { max = Math.floor(max) if (min) { min = Math.ceil(min) @@ -24,7 +22,7 @@ function getSecureRandomInteger (max, min = 0) { * @param min * @returns */ -function getRandomInteger (max, min = 0) { +function getRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { max = Math.floor(max) if (min) { min = Math.ceil(min) @@ -33,21 +31,16 @@ function getRandomInteger (max, min = 0) { 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', function () { getSecureRandomInteger(maximum) - }) - .add('Random integer generator', function () { + }), + Benchmark.add('Random integer generator', function () { getRandomInteger(maximum) - }) - .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() + }), + Benchmark.cycle(), + Benchmark.complete(), + Benchmark.save({ file: 'random-integer-generator', format: 'chart.html' }), + Benchmark.save({ file: 'random-integer-generator', format: 'table.html' }) +)