X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=busy-wait.js;fp=busy-wait.js;h=66d58967ed9e86c0cf00b61708379dc46f2e0be4;hb=eab47c66b6e0c8c9a6d2667cd5fbc17190f24485;hp=9d0d31b4d6ee0285a9fe7fcd19a54e96e17de913;hpb=0b45b8b3243805e1c055e824a35e466bcd7fc3a1;p=benchmarks-js.git 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(),