1 const Benchmark
= require('benchmark')
2 const { LIST_FORMATTER
, sleep
} = require('./benchmark-utils')
4 const suite
= new Benchmark
.Suite()
11 function dummyTimeoutBusyWait (timeoutMs
) {
12 const timeoutTimestampMs
= Date
.now() + timeoutMs
13 do {} while (Date
.now() < timeoutTimestampMs
)
20 async
function divideAndConquerTimeoutBusyWait (timeoutMs
, delayMs
= 200) {
21 const tries
= Math
.round(timeoutMs
/ delayMs
)
26 } while (count
<= tries
)
33 function setIntervalTimeoutBusyWait (timeoutMs
, delayMs
= 200) {
34 const tries
= Math
.round(timeoutMs
/ delayMs
)
36 const triesSetInterval
= setInterval(() => {
38 if (count
=== tries
) {
39 clearInterval(triesSetInterval
)
45 .add('dummyTimeoutBusyWait', function () {
46 dummyTimeoutBusyWait(timeout
)
48 .add('divideAndConquerTimeoutBusyWait', async
function () {
49 await
divideAndConquerTimeoutBusyWait(timeout
)
51 .add('setIntervalTimeoutBusyWait', function () {
52 setIntervalTimeoutBusyWait(timeout
)
54 .on('cycle', function (event
) {
55 console
.log(event
.target
.toString())
57 .on('complete', function () {
59 'Fastest is ' + LIST_FORMATTER
.format(this.filter('fastest').map('name'))
61 // eslint-disable-next-line no-process-exit