Initial benckmark commit
[benchmarks-js.git] / busy-wait.js
CommitLineData
ed2968f2
JB
1const Benchmark = require('benchmark')
2const { LIST_FORMATTER } = require('./benchmark-utils')
3
4const suite = new Benchmark.Suite()
5
6const timeout = 2000
7
8function dummyTimeoutBusyWait (timeoutMs) {
9 const timeoutDateMs = Date.now() + timeoutMs
10 do {} while (Date.now() < timeoutDateMs)
11}
12
13function setIntervalTimeoutBusyWait (timeoutMs, delayMs = 200) {
14 const tries = Math.round(timeoutMs / delayMs)
15 let count = 0
16 const triesSetInterval = setInterval(() => {
17 count++
18 if (count === tries) {
19 clearInterval(triesSetInterval)
20 }
21 }, delayMs)
22}
23
24suite
25 .add('dummyTimeoutBusyWait', function () {
26 dummyTimeoutBusyWait(timeout)
27 })
28 .add('setIntervalTimeoutBusyWait', function () {
29 setIntervalTimeoutBusyWait(timeout)
30 })
31 .on('cycle', function (event) {
32 console.log(event.target.toString())
33 })
34 .on('complete', function () {
35 console.log(
36 'Fastest is ' + LIST_FORMATTER.format(this.filter('fastest').map('name'))
37 )
38 // eslint-disable-next-line no-process-exit
39 process.exit()
40 })
41 .run()