1 const Benchmark
= require('benny')
2 const { sleep
} = require('./benchmark-utils')
10 function dummyTimeoutBusyWait (timeoutMs
) {
11 const timeoutTimestampMs
= Date
.now() + timeoutMs
12 // eslint-disable-next-line no-empty
13 do {} while (Date
.now() < timeoutTimestampMs
)
19 async
function sleepTimeoutBusyWait (timeoutMs
) {
20 const timeoutTimestampMs
= Date
.now() + timeoutMs
23 } while (Date
.now() < timeoutTimestampMs
)
30 async
function divideAndConquerTimeoutBusyWait (
34 const tries
= Math
.round(timeoutMs
/ intervalMs
)
38 await
sleep(intervalMs
)
39 } while (count
<= tries
)
46 function setIntervalTimeoutBusyWait (timeoutMs
, intervalMs
= interval
) {
47 const tries
= Math
.round(timeoutMs
/ intervalMs
)
49 const triesSetInterval
= setInterval(() => {
51 if (count
=== tries
) {
52 clearInterval(triesSetInterval
)
59 Benchmark
.add('dummyTimeoutBusyWait', () => {
60 dummyTimeoutBusyWait(timeout
)
62 Benchmark
.add('sleepTimeoutBusyWait', async () => {
63 await
sleepTimeoutBusyWait(timeout
)
65 Benchmark
.add('divideAndConquerTimeoutBusyWait', async () => {
66 await
divideAndConquerTimeoutBusyWait(timeout
)
68 Benchmark
.add('setIntervalTimeoutBusyWait', () => {
69 setIntervalTimeoutBusyWait(timeout
)
73 Benchmark
.save({ file
: 'busy-wait', format
: 'json', details
: true }),
74 Benchmark
.save({ file
: 'busy-wait', format
: 'chart.html', details
: true }),
75 Benchmark
.save({ file
: 'busy-wait', format
: 'table.html', details
: true })