From a9c78d5d3394dce4eccd9f244c88879666cba677 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 6 Jun 2021 22:03:18 +0200 Subject: [PATCH] Add more benchmarks MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- benchmark-utils.js | 9 +++++- busy-wait.js | 22 +++++++++++++-- fibonacci.js | 70 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 4 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 fibonacci.js diff --git a/benchmark-utils.js b/benchmark-utils.js index c1b68c5..4de6ea7 100644 --- a/benchmark-utils.js +++ b/benchmark-utils.js @@ -9,9 +9,16 @@ function generateRandomInteger (max, min = 0) { return Math.floor(Math.random() * max + 1) } +/** + * @param ms + */ +async function sleep (ms) { + return new Promise(resolve => setTimeout(resolve, ms)) +} + const LIST_FORMATTER = new Intl.ListFormat('en-US', { style: 'long', type: 'conjunction' }) -module.exports = { generateRandomInteger, LIST_FORMATTER } +module.exports = { generateRandomInteger, sleep, LIST_FORMATTER } diff --git a/busy-wait.js b/busy-wait.js index ed06c52..9ff5a00 100644 --- a/busy-wait.js +++ b/busy-wait.js @@ -1,5 +1,5 @@ const Benchmark = require('benchmark') -const { LIST_FORMATTER } = require('./benchmark-utils') +const { LIST_FORMATTER, sleep } = require('./benchmark-utils') const suite = new Benchmark.Suite() @@ -9,8 +9,21 @@ const timeout = 2000 * @param timeoutMs */ function dummyTimeoutBusyWait (timeoutMs) { - const timeoutDateMs = Date.now() + timeoutMs - do {} while (Date.now() < timeoutDateMs) + const timeoutTimestampMs = Date.now() + timeoutMs + do {} while (Date.now() < timeoutTimestampMs) +} + +/** + * @param timeoutMs + * @param delayMs + */ +async function divideAndConquerTimeoutBusyWait (timeoutMs, delayMs = 200) { + const tries = Math.round(timeoutMs / delayMs) + let count = 0 + do { + count++ + await sleep(delayMs) + } while (count <= tries) } /** @@ -32,6 +45,9 @@ suite .add('dummyTimeoutBusyWait', function () { dummyTimeoutBusyWait(timeout) }) + .add('divideAndConquerTimeoutBusyWait', async function () { + await divideAndConquerTimeoutBusyWait(timeout) + }) .add('setIntervalTimeoutBusyWait', function () { setIntervalTimeoutBusyWait(timeout) }) diff --git a/fibonacci.js b/fibonacci.js new file mode 100644 index 0000000..b5a7fbb --- /dev/null +++ b/fibonacci.js @@ -0,0 +1,70 @@ +const Benchmark = require('benchmark') +const { LIST_FORMATTER } = require('./benchmark-utils') + +const suite = new Benchmark.Suite() + +const number = 30 + +/** + * @param num + */ +function fibonacciLoop (num) { + let a = 1 + let b = 0 + let temp + + while (num >= 0) { + temp = a + a = a + b + b = temp + num-- + } + + return b +} + +/** + * @param num + */ +function fibonacciRecursion (num) { + if (num <= 1) return 1 + + return fibonacciRecursion(num - 1) + fibonacciRecursion(num - 2) +} + +/** + * @param num + * @param memo + */ +function fibonacciRecursionMemoization (num, memo) { + memo = memo || {} + + if (memo[num]) return memo[num] + if (num <= 1) return 1 + + return (memo[num] = + fibonacciRecursionMemoization(num - 1, memo) + + fibonacciRecursionMemoization(num - 2, memo)) +} + +suite + .add('fibonacciLoop', function () { + fibonacciLoop(number) + }) + .add('fibonacciRecursion', function () { + fibonacciRecursion(number) + }) + .add('fibonacciRecursionMemoization', function () { + fibonacciRecursionMemoization(number) + }) + .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() diff --git a/package.json b/package.json index eaec39c..c85ef5d 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "benchmark:busy-wait": "node busy-wait.js", "benchmark:quick-select": "node quick-select.js", "benchmark:promise-handling": "node promise-handling.js", + "benchmark:fibonacci": "node fibonacci.js", "format": "prettier --loglevel silent --write .; prettierx --write .", "lint": "eslint .", "lint:fix": "eslint . --fix", -- 2.34.1