From 7e8612886ac3d42901e452b0bd36781410f37b58 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 24 Oct 2022 18:10:13 +0200 Subject: [PATCH] Convert more benchmark to benny MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- promise-handling.js | 47 ++++++++++++++++++++++++++------------------- random.js | 6 ++++++ 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/promise-handling.js b/promise-handling.js index e4e712b..002be6d 100644 --- a/promise-handling.js +++ b/promise-handling.js @@ -1,13 +1,12 @@ -const Benchmark = require('benchmark') -const { LIST_FORMATTER } = require('./benchmark-utils') - -const suite = new Benchmark.Suite() +const Benchmark = require('benny') /** * */ function promise () { - return new Promise() + return new Promise(resolve => { + resolve() + }) } /** @@ -17,21 +16,29 @@ async function asyncFunction () { await promise() } -suite - .add('await promise', async () => { +Benchmark.suite( + 'Promise handling', + Benchmark.add('await promise', async () => { await asyncFunction() - }) - .add('promise', () => { + }), + Benchmark.add('promise', () => { asyncFunction() + }), + Benchmark.cycle(), + Benchmark.complete(), + Benchmark.save({ + file: 'promise-handling', + format: 'json', + details: true + }), + Benchmark.save({ + file: 'promise-handling', + format: 'chart.html', + details: true + }), + Benchmark.save({ + file: 'promise-handling', + format: 'table.html', + details: true }) - .on('cycle', 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() +) diff --git a/random.js b/random.js index ee5cc61..f4fd9f0 100644 --- a/random.js +++ b/random.js @@ -9,6 +9,9 @@ const maximum = Number.MAX_SAFE_INTEGER * @returns */ 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 != null && min !== 0) { min = Math.ceil(min) @@ -23,6 +26,9 @@ function getSecureRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { * @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) -- 2.34.1