1 import { bench, group, run } from 'tatami-ng'
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)
62 group('Busy wait', () => {
63 bench('dummyTimeoutBusyWait', () => {
64 dummyTimeoutBusyWait(timeout)
66 bench('sleepTimeoutBusyWait', async () => {
67 await sleepTimeoutBusyWait(timeout)
69 bench('divideAndConquerTimeoutBusyWait', async () => {
70 await divideAndConquerTimeoutBusyWait(timeout)
72 bench('setIntervalTimeoutBusyWait', async () => {
73 await setIntervalTimeoutBusyWait(timeout)