X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=busy-wait.js;h=a851a30e911870542cd9612b2777c7654c057a36;hb=05a1f5196369849f4e993f66062d5bbafbd9d5d9;hp=66d58967ed9e86c0cf00b61708379dc46f2e0be4;hpb=823ca29667e5d5e4951f629ebe19ae8ec1f44b52;p=benchmarks-js.git diff --git a/busy-wait.js b/busy-wait.js index 66d5896..a851a30 100644 --- a/busy-wait.js +++ b/busy-wait.js @@ -8,9 +8,9 @@ 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) } /** @@ -18,10 +18,10 @@ function dummyTimeoutBusyWait (timeoutMs) { * @param intervalMs */ async function sleepTimeoutBusyWait (timeoutMs, intervalMs = interval) { - const timeoutTimestampMs = Date.now() + timeoutMs + const timeoutTimestampMs = performance.now() + timeoutMs do { await sleep(intervalMs) - } while (Date.now() < timeoutTimestampMs) + } while (performance.now() < timeoutTimestampMs) } /** @@ -45,15 +45,17 @@ async function divideAndConquerTimeoutBusyWait ( * @param intervalMs */ 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) + 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( @@ -75,4 +77,6 @@ Benchmark.suite( 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) +})