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