build(deps-dev): apply updates
[benchmarks-js.git] / busy-wait.js
index 9d0d31b4d6ee0285a9fe7fcd19a54e96e17de913..a851a30e911870542cd9612b2777c7654c057a36 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,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)
+})