1 const Benchmark
= require('benny')
2 const { sleep
} = require('./benchmark-utils')
10 function dummyTimeoutBusyWait (timeoutMs
) {
11 const timeoutTimestampMs
= performance
.now() + timeoutMs
12 // eslint-disable-next-line no-empty
13 do {} while (performance
.now() < timeoutTimestampMs
)
20 async
function sleepTimeoutBusyWait (timeoutMs
, intervalMs
= interval
) {
21 const timeoutTimestampMs
= performance
.now() + timeoutMs
23 await
sleep(intervalMs
)
24 } while (performance
.now() < timeoutTimestampMs
)
31 async
function divideAndConquerTimeoutBusyWait (
35 const tries
= Math
.round(timeoutMs
/ intervalMs
)
39 await
sleep(intervalMs
)
40 } while (count
<= tries
)
47 async
function setIntervalTimeoutBusyWait (timeoutMs
, intervalMs
= interval
) {
48 return new Promise(resolve
=> {
49 const tries
= Math
.round(timeoutMs
/ intervalMs
)
51 const triesSetInterval
= setInterval(() => {
53 if (count
=== tries
) {
54 clearInterval(triesSetInterval
)
63 Benchmark
.add('dummyTimeoutBusyWait', () => {
64 dummyTimeoutBusyWait(timeout
)
66 Benchmark
.add('sleepTimeoutBusyWait', async () => {
67 await
sleepTimeoutBusyWait(timeout
)
69 Benchmark
.add('divideAndConquerTimeoutBusyWait', async () => {
70 await
divideAndConquerTimeoutBusyWait(timeout
)
72 Benchmark
.add('setIntervalTimeoutBusyWait', async () => {
73 await
setIntervalTimeoutBusyWait(timeout
)
77 Benchmark
.save({ file
: 'busy-wait', format
: 'json', details
: true }),
78 Benchmark
.save({ file
: 'busy-wait', format
: 'chart.html', details
: true }),
79 Benchmark
.save({ file
: 'busy-wait', format
: 'table.html', details
: true })