Pin volta pnpm version
[benchmarks-js.git] / busy-wait.js
index 9d0d31b4d6ee0285a9fe7fcd19a54e96e17de913..9c392a53c06755d1e1d3f321f9c6ae176d14d3b5 100644 (file)
@@ -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(),