build: bump volta pnpm version
[benchmarks-js.git] / busy-wait.js
index 4742018a698db86fb50aadb5656d42fde29d8f56..a851a30e911870542cd9612b2777c7654c057a36 100644 (file)
@@ -1,41 +1,82 @@
-const Benchmark = require('benchmark')
-const { LIST_FORMATTER } = require('./benchmark-utils')
-
-const suite = new Benchmark.Suite()
+const Benchmark = require('benny')
+const { sleep } = require('./benchmark-utils')
 
 const timeout = 2000
+const interval = 1000
 
+/**
+ * @param timeoutMs
+ */
 function dummyTimeoutBusyWait (timeoutMs) {
-  const timeoutDateMs = Date.now() + timeoutMs
-  do {} while (Date.now() < timeoutDateMs)
+  const timeoutTimestampMs = performance.now() + timeoutMs
+  // eslint-disable-next-line no-empty
+  do {} while (performance.now() < timeoutTimestampMs)
+}
+
+/**
+ * @param timeoutMs
+ * @param intervalMs
+ */
+async function sleepTimeoutBusyWait (timeoutMs, intervalMs = interval) {
+  const timeoutTimestampMs = performance.now() + timeoutMs
+  do {
+    await sleep(intervalMs)
+  } while (performance.now() < timeoutTimestampMs)
 }
 
-function setIntervalTimeoutBusyWait (timeoutMs, delayMs = 200) {
-  const tries = Math.round(timeoutMs / delayMs)
+/**
+ * @param timeoutMs
+ * @param intervalMs
+ */
+async function divideAndConquerTimeoutBusyWait (
+  timeoutMs,
+  intervalMs = interval
+) {
+  const tries = Math.round(timeoutMs / intervalMs)
   let count = 0
-  const triesSetInterval = setInterval(() => {
+  do {
     count++
-    if (count === tries) {
-      clearInterval(triesSetInterval)
-    }
-  }, delayMs)
+    await sleep(intervalMs)
+  } while (count <= tries)
 }
 
-suite
-  .add('dummyTimeoutBusyWait', function () {
-    dummyTimeoutBusyWait(timeout)
-  })
-  .add('setIntervalTimeoutBusyWait', function () {
-    setIntervalTimeoutBusyWait(timeout)
+/**
+ * @param timeoutMs
+ * @param 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)
   })
-  .on('cycle', function (event) {
-    console.log(event.target.toString())
-  })
-  .on('complete', function () {
-    console.log(
-      'Fastest is ' + LIST_FORMATTER.format(this.filter('fastest').map('name'))
-    )
-    // eslint-disable-next-line no-process-exit
-    process.exit()
-  })
-  .run()
+}
+
+Benchmark.suite(
+  'Busy wait',
+  Benchmark.add('dummyTimeoutBusyWait', () => {
+    dummyTimeoutBusyWait(timeout)
+  }),
+  Benchmark.add('sleepTimeoutBusyWait', async () => {
+    await sleepTimeoutBusyWait(timeout)
+  }),
+  Benchmark.add('divideAndConquerTimeoutBusyWait', async () => {
+    await divideAndConquerTimeoutBusyWait(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)
+})