1 const Benchmark
= require('benchmark')
2 const { LIST_FORMATTER
, sleep
} = require('./benchmark-utils')
4 const suite
= new Benchmark
.Suite()
12 async
function sleepTimeoutBusyWait (timeoutMs
) {
13 const timeoutTimestampMs
= Date
.now() + timeoutMs
16 } while (Date
.now() < timeoutTimestampMs
)
23 async
function divideAndConquerTimeoutBusyWait (
27 const tries
= Math
.round(timeoutMs
/ intervalMs
)
31 await
sleep(intervalMs
)
32 } while (count
<= tries
)
39 function setIntervalTimeoutBusyWait (timeoutMs
, intervalMs
= interval
) {
40 const tries
= Math
.round(timeoutMs
/ intervalMs
)
42 const triesSetInterval
= setInterval(() => {
44 if (count
=== tries
) {
45 clearInterval(triesSetInterval
)
51 .add('sleepTimeoutBusyWait', async
function () {
52 sleepTimeoutBusyWait(timeout
)
54 .add('divideAndConquerTimeoutBusyWait', async
function () {
55 await
divideAndConquerTimeoutBusyWait(timeout
)
57 .add('setIntervalTimeoutBusyWait', function () {
58 setIntervalTimeoutBusyWait(timeout
)
60 .on('cycle', function (event
) {
61 console
.log(event
.target
.toString())
63 .on('complete', function () {
65 'Fastest is ' + LIST_FORMATTER
.format(this.filter('fastest').map('name'))
67 // eslint-disable-next-line no-process-exit