From 2deff321e347e2ca6c419c5d98b82fe2f5595a03 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 22 Oct 2022 10:38:59 +0200 Subject: [PATCH] Add empty array benchmark MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- busy-wait.js | 10 +++++----- empty-array.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ fibonacci.js | 8 ++++---- package.json | 1 + 4 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 empty-array.js diff --git a/busy-wait.js b/busy-wait.js index f2d10f3..46e5853 100644 --- a/busy-wait.js +++ b/busy-wait.js @@ -57,19 +57,19 @@ function setIntervalTimeoutBusyWait (timeoutMs, intervalMs = interval) { } suite - .add('dummyTimeoutBusyWait', function () { + .add('dummyTimeoutBusyWait', () => { dummyTimeoutBusyWait(timeout) }) - .add('sleepTimeoutBusyWait', async function () { + .add('sleepTimeoutBusyWait', async () => { sleepTimeoutBusyWait(timeout) }) - .add('divideAndConquerTimeoutBusyWait', async function () { + .add('divideAndConquerTimeoutBusyWait', async () => { await divideAndConquerTimeoutBusyWait(timeout) }) - .add('setIntervalTimeoutBusyWait', function () { + .add('setIntervalTimeoutBusyWait', () => { setIntervalTimeoutBusyWait(timeout) }) - .on('cycle', function (event) { + .on('cycle', event => { console.log(event.target.toString()) }) .on('complete', function () { diff --git a/empty-array.js b/empty-array.js new file mode 100644 index 0000000..9f93d32 --- /dev/null +++ b/empty-array.js @@ -0,0 +1,51 @@ +const Benchmark = require('benchmark') +const { LIST_FORMATTER } = require('./benchmark-utils') + +const suite = new Benchmark.Suite() + +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 +] + +suite + .add('length = 0', () => { + testArray.length = 0 + }) + .add('pop loop', async () => { + while (testArray.length > 0) { + testArray.pop() + } + }) + .add('splice', async () => { + testArray.splice(0, testArray.length) + }) + .add('shift loop', () => { + while (testArray.length > 0) { + testArray.shift() + } + }) + .add('new init', () => { + testArray = [] + }) + .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/fibonacci.js b/fibonacci.js index 4a52de4..729fe92 100644 --- a/fibonacci.js +++ b/fibonacci.js @@ -51,16 +51,16 @@ function fibonacciRecursionMemoization (num, memo) { } suite - .add('fibonacciLoop', function () { + .add('fibonacciLoop', () => { fibonacciLoop(number) }) - .add('fibonacciRecursion', function () { + .add('fibonacciRecursion', () => { fibonacciRecursion(number) }) - .add('fibonacciRecursionMemoization', function () { + .add('fibonacciRecursionMemoization', () => { fibonacciRecursionMemoization(number) }) - .on('cycle', function (event) { + .on('cycle', event => { console.log(event.target.toString()) }) .on('complete', function () { diff --git a/package.json b/package.json index 5c54c47..ed3240f 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ }, "scripts": { "benchmark:busy-wait": "node busy-wait.js", + "benchmark:empty-array": "node empty-array.js", "benchmark:quick-select": "node quick-select.js", "benchmark:promise-handling": "node promise-handling.js", "benchmark:fibonacci": "node fibonacci.js", -- 2.34.1