From ed40d2b012a740d121f1951b9be91df341d6eaf3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 23 Oct 2022 23:08:04 +0200 Subject: [PATCH] Improve random integer array generation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- benchmark-utils.js | 21 ++++++++++++++++++++- busy-wait.js | 2 +- empty-array.js | 16 ++-------------- max.js | 16 ++-------------- 4 files changed, 25 insertions(+), 30 deletions(-) diff --git a/benchmark-utils.js b/benchmark-utils.js index 56689d6..1e7914f 100644 --- a/benchmark-utils.js +++ b/benchmark-utils.js @@ -29,6 +29,19 @@ function generateRandomInteger (max, min = 0) { return Math.floor(secureRandom() * (max + 1)) } +/** + * + * @param size + * @param max + */ +function generateRandomIntegerArray (size, max = Number.MAX_SAFE_INTEGER) { + const integerArray = [] + for (let i = 0; i < size; i++) { + integerArray.push(generateRandomInteger(max)) + } + return integerArray +} + /** * @param ms * @returns @@ -42,4 +55,10 @@ const LIST_FORMATTER = new Intl.ListFormat('en-US', { type: 'conjunction' }) -module.exports = { generateRandomInteger, sleep, secureRandom, LIST_FORMATTER } +module.exports = { + generateRandomInteger, + generateRandomIntegerArray, + sleep, + secureRandom, + LIST_FORMATTER +} diff --git a/busy-wait.js b/busy-wait.js index 46e5853..eece669 100644 --- a/busy-wait.js +++ b/busy-wait.js @@ -61,7 +61,7 @@ suite dummyTimeoutBusyWait(timeout) }) .add('sleepTimeoutBusyWait', async () => { - sleepTimeoutBusyWait(timeout) + await sleepTimeoutBusyWait(timeout) }) .add('divideAndConquerTimeoutBusyWait', async () => { await divideAndConquerTimeoutBusyWait(timeout) diff --git a/empty-array.js b/empty-array.js index e623b5e..9ddc17d 100644 --- a/empty-array.js +++ b/empty-array.js @@ -1,19 +1,7 @@ const Benchmark = require('benny') +const { generateRandomIntegerArray } = require('./benchmark-utils') -let testArray = [ - 83, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, - 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, - 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, - 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, - 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, - 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, - 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, - 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, - 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, - 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, - 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, - 36, 28 -] +let testArray = generateRandomIntegerArray(10000) Benchmark.suite( 'Empty array', diff --git a/max.js b/max.js index d0d1381..5525c92 100644 --- a/max.js +++ b/max.js @@ -1,19 +1,7 @@ const Benchmark = require('benny') +const { generateRandomIntegerArray } = require('./benchmark-utils') -const testArray = [ - 83, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, - 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, - 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, - 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, - 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, - 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, - 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, - 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, - 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, - 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, - 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, - 36, 28 -] +const testArray = generateRandomIntegerArray(10000) /** * -- 2.34.1