1 import Benchmark from 'benny'
3 import { sleep } from './benchmark-utils.mjs'
11 function dummyTimeoutBusyWait (timeoutMs) {
12 const timeoutTimestampMs = performance.now() + timeoutMs
13 // eslint-disable-next-line no-empty
14 do {} while (performance.now() < timeoutTimestampMs)
21 async function sleepTimeoutBusyWait (timeoutMs, intervalMs = interval) {
22 const timeoutTimestampMs = performance.now() + timeoutMs
24 await sleep(intervalMs)
25 } while (performance.now() < timeoutTimestampMs)
32 async function divideAndConquerTimeoutBusyWait (
36 const tries = Math.round(timeoutMs / intervalMs)
40 await sleep(intervalMs)
41 } while (count <= tries)
48 async function setIntervalTimeoutBusyWait (timeoutMs, intervalMs = interval) {
49 await new Promise(resolve => {
50 const tries = Math.round(timeoutMs / intervalMs)
52 const triesSetInterval = setInterval(() => {
54 if (count === tries) {
55 clearInterval(triesSetInterval)
64 Benchmark.add('dummyTimeoutBusyWait', () => {
65 dummyTimeoutBusyWait(timeout)
67 Benchmark.add('sleepTimeoutBusyWait', async () => {
68 await sleepTimeoutBusyWait(timeout)
70 Benchmark.add('divideAndConquerTimeoutBusyWait', async () => {
71 await divideAndConquerTimeoutBusyWait(timeout)
73 Benchmark.add('setIntervalTimeoutBusyWait', async () => {
74 await setIntervalTimeoutBusyWait(timeout)
78 Benchmark.save({ file: 'busy-wait', format: 'json', details: true }),
79 Benchmark.save({ file: 'busy-wait', format: 'chart.html', details: true }),
80 Benchmark.save({ file: 'busy-wait', format: 'table.html', details: true })
81 ).catch(console.error)