Add more benchmarks
[benchmarks-js.git] / busy-wait.js
index ed06c52a682ab408449f273e1fe03333917ac0bc..9ff5a000f72463fcb600c45644e8f95deb4e05dd 100644 (file)
@@ -1,5 +1,5 @@
 const Benchmark = require('benchmark')
-const { LIST_FORMATTER } = require('./benchmark-utils')
+const { LIST_FORMATTER, sleep } = require('./benchmark-utils')
 
 const suite = new Benchmark.Suite()
 
@@ -9,8 +9,21 @@ const timeout = 2000
  * @param timeoutMs
  */
 function dummyTimeoutBusyWait (timeoutMs) {
-  const timeoutDateMs = Date.now() + timeoutMs
-  do {} while (Date.now() < timeoutDateMs)
+  const timeoutTimestampMs = Date.now() + timeoutMs
+  do {} while (Date.now() < timeoutTimestampMs)
+}
+
+/**
+ * @param timeoutMs
+ * @param delayMs
+ */
+async function divideAndConquerTimeoutBusyWait (timeoutMs, delayMs = 200) {
+  const tries = Math.round(timeoutMs / delayMs)
+  let count = 0
+  do {
+    count++
+    await sleep(delayMs)
+  } while (count <= tries)
 }
 
 /**
@@ -32,6 +45,9 @@ suite
   .add('dummyTimeoutBusyWait', function () {
     dummyTimeoutBusyWait(timeout)
   })
+  .add('divideAndConquerTimeoutBusyWait', async function () {
+    await divideAndConquerTimeoutBusyWait(timeout)
+  })
   .add('setIntervalTimeoutBusyWait', function () {
     setIntervalTimeoutBusyWait(timeout)
   })