From a8bf8b7d73343b33657cb8d6f36091a3bcdc38d6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 8 Jun 2021 13:18:10 +0200 Subject: [PATCH] Busy wait with a sleep inside. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- busy-wait.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/busy-wait.js b/busy-wait.js index 5ea20aa..d49a5fd 100644 --- a/busy-wait.js +++ b/busy-wait.js @@ -4,20 +4,26 @@ const { LIST_FORMATTER, sleep } = require('./benchmark-utils') const suite = new Benchmark.Suite() const timeout = 2000 +const interval = 1000 /** * @param timeoutMs */ -function dummyTimeoutBusyWait (timeoutMs) { +async function sleepTimeoutBusyWait (timeoutMs) { const timeoutTimestampMs = Date.now() + timeoutMs - do {} while (Date.now() < timeoutTimestampMs) + do { + await sleep(interval) + } while (Date.now() < timeoutTimestampMs) } /** * @param timeoutMs * @param intervalMs */ -async function divideAndConquerTimeoutBusyWait (timeoutMs, intervalMs = 200) { +async function divideAndConquerTimeoutBusyWait ( + timeoutMs, + intervalMs = interval +) { const tries = Math.round(timeoutMs / intervalMs) let count = 0 do { @@ -30,7 +36,7 @@ async function divideAndConquerTimeoutBusyWait (timeoutMs, intervalMs = 200) { * @param timeoutMs * @param intervalMs */ -function setIntervalTimeoutBusyWait (timeoutMs, intervalMs = 200) { +function setIntervalTimeoutBusyWait (timeoutMs, intervalMs = interval) { const tries = Math.round(timeoutMs / intervalMs) let count = 0 const triesSetInterval = setInterval(() => { @@ -42,8 +48,8 @@ function setIntervalTimeoutBusyWait (timeoutMs, intervalMs = 200) { } suite - .add('dummyTimeoutBusyWait', function () { - dummyTimeoutBusyWait(timeout) + .add('sleepTimeoutBusyWait', async function () { + sleepTimeoutBusyWait(timeout) }) .add('divideAndConquerTimeoutBusyWait', async function () { await divideAndConquerTimeoutBusyWait(timeout) -- 2.34.1