X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=busy-wait.js;h=a851a30e911870542cd9612b2777c7654c057a36;hb=38acce1ce538dd398196b8ab7834d55938d4fde7;hp=9d0d31b4d6ee0285a9fe7fcd19a54e96e17de913;hpb=662a730a20d42b0057cf65d1baeb77982e29d3ba;p=benchmarks-js.git diff --git a/busy-wait.js b/busy-wait.js index 9d0d31b..a851a30 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,15 +44,18 @@ async function divideAndConquerTimeoutBusyWait ( * @param timeoutMs * @param intervalMs */ -function setIntervalTimeoutBusyWait (timeoutMs, intervalMs = interval) { - const tries = Math.round(timeoutMs / intervalMs) - let count = 0 - const triesSetInterval = setInterval(() => { - count++ - if (count === tries) { - clearInterval(triesSetInterval) - } - }, intervalMs) +async function setIntervalTimeoutBusyWait (timeoutMs, intervalMs = interval) { + return new Promise(resolve => { + const tries = Math.round(timeoutMs / intervalMs) + let count = 0 + const triesSetInterval = setInterval(() => { + count++ + if (count === tries) { + clearInterval(triesSetInterval) + return resolve() + } + }, intervalMs) + }) } Benchmark.suite( @@ -65,12 +69,14 @@ 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(), Benchmark.save({ file: 'busy-wait', format: 'json', details: true }), Benchmark.save({ file: 'busy-wait', format: 'chart.html', details: true }), Benchmark.save({ file: 'busy-wait', format: 'table.html', details: true }) -) +).catch(err => { + console.error(err) +})