X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=busy-wait.js;h=9c392a53c06755d1e1d3f321f9c6ae176d14d3b5;hb=1c0529a4522c118ce1f603a684508593b056a77b;hp=9d0d31b4d6ee0285a9fe7fcd19a54e96e17de913;hpb=662a730a20d42b0057cf65d1baeb77982e29d3ba;p=benchmarks-js.git diff --git a/busy-wait.js b/busy-wait.js index 9d0d31b..9c392a5 100644 --- a/busy-wait.js +++ b/busy-wait.js @@ -8,19 +8,20 @@ const interval = 1000 * @param timeoutMs */ function dummyTimeoutBusyWait (timeoutMs) { - const timeoutTimestampMs = Date.now() + timeoutMs + const timeoutTimestampMs = performance.now() + timeoutMs // eslint-disable-next-line no-empty - do {} while (Date.now() < timeoutTimestampMs) + do {} while (performance.now() < timeoutTimestampMs) } /** * @param timeoutMs + * @param intervalMs */ -async function sleepTimeoutBusyWait (timeoutMs) { - const timeoutTimestampMs = Date.now() + timeoutMs +async function sleepTimeoutBusyWait (timeoutMs, intervalMs = interval) { + const timeoutTimestampMs = performance.now() + timeoutMs do { - await sleep(interval) - } while (Date.now() < timeoutTimestampMs) + await sleep(intervalMs) + } while (performance.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(),