From f10b1da0dc4d21b8b8b8ba91283809028dd710e9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 23 Oct 2022 23:20:10 +0200 Subject: [PATCH] Use benny in random integer generator benchmark MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- benchmark-utils.js | 2 +- random.js | 39 ++++++++++++++++----------------------- 2 files changed, 17 insertions(+), 24 deletions(-) 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' }) +) -- 2.34.1