From: Jérôme Benoit Date: Mon, 28 Nov 2022 22:17:15 +0000 (+0100) Subject: Ensure busy wait does not end before promise fullfiling X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=eab47c66b6e0c8c9a6d2667cd5fbc17190f24485;p=benchmarks-js.git Ensure busy wait does not end before promise fullfiling Signed-off-by: Jérôme Benoit --- diff --git a/busy-wait.js b/busy-wait.js index 9d0d31b..66d5896 100644 --- a/busy-wait.js +++ b/busy-wait.js @@ -15,11 +15,12 @@ function dummyTimeoutBusyWait (timeoutMs) { /** * @param timeoutMs + * @param intervalMs */ -async function sleepTimeoutBusyWait (timeoutMs) { +async function sleepTimeoutBusyWait (timeoutMs, intervalMs = interval) { const timeoutTimestampMs = Date.now() + timeoutMs do { - await sleep(interval) + await sleep(intervalMs) } while (Date.now() < timeoutTimestampMs) } @@ -43,13 +44,14 @@ async function divideAndConquerTimeoutBusyWait ( * @param timeoutMs * @param intervalMs */ -function setIntervalTimeoutBusyWait (timeoutMs, intervalMs = interval) { +async function setIntervalTimeoutBusyWait (timeoutMs, intervalMs = interval) { const tries = Math.round(timeoutMs / intervalMs) let count = 0 const triesSetInterval = setInterval(() => { count++ if (count === tries) { clearInterval(triesSetInterval) + return Promise.resolve() } }, intervalMs) } @@ -65,8 +67,8 @@ Benchmark.suite( Benchmark.add('divideAndConquerTimeoutBusyWait', async () => { await divideAndConquerTimeoutBusyWait(timeout) }), - Benchmark.add('setIntervalTimeoutBusyWait', () => { - setIntervalTimeoutBusyWait(timeout) + Benchmark.add('setIntervalTimeoutBusyWait', async () => { + await setIntervalTimeoutBusyWait(timeout) }), Benchmark.cycle(), Benchmark.complete(),