Commit | Line | Data |
---|---|---|
aacd8188 | 1 | const Benchmark = require('benchmark') |
d1a9aa41 | 2 | const { LIST_FORMATTER } = require('../benchmarks-utils') |
aacd8188 S |
3 | |
4 | const suite = new Benchmark.Suite() | |
5 | ||
292ad316 JB |
6 | function generateWorkersArray (numberOfWorkers) { |
7 | return [...Array(numberOfWorkers).keys()] | |
8 | } | |
aacd8188 | 9 | |
292ad316 | 10 | const workers = generateWorkersArray(60) |
aacd8188 | 11 | |
e843b904 | 12 | let nextWorkerIndex |
aacd8188 | 13 | |
0d6f0c13 | 14 | function roundRobinTernaryOffByOne () { |
aacd8188 S |
15 | nextWorkerIndex = |
16 | workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1 | |
17 | return workers[nextWorkerIndex] | |
18 | } | |
19 | ||
0d6f0c13 | 20 | function roundRobinTernaryWithNegation () { |
472bf1f5 JB |
21 | nextWorkerIndex = |
22 | !nextWorkerIndex || workers.length - 1 === nextWorkerIndex | |
23 | ? 0 | |
24 | : nextWorkerIndex + 1 | |
25 | return workers[nextWorkerIndex] | |
26 | } | |
27 | ||
0d6f0c13 | 28 | function roundRobinTernaryWithPreChoosing () { |
aacd8188 S |
29 | const chosenWorker = workers[nextWorkerIndex] |
30 | nextWorkerIndex = | |
31 | workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1 | |
32 | return chosenWorker | |
33 | } | |
34 | ||
0d6f0c13 | 35 | function roundRobinIncrementModulo () { |
aacd8188 S |
36 | const chosenWorker = workers[nextWorkerIndex] |
37 | nextWorkerIndex++ | |
38 | nextWorkerIndex %= workers.length | |
39 | return chosenWorker | |
40 | } | |
41 | ||
42 | suite | |
ff5e76e1 | 43 | .add('Ternary off by one', function () { |
aacd8188 | 44 | nextWorkerIndex = 0 |
0d6f0c13 | 45 | roundRobinTernaryOffByOne() |
aacd8188 | 46 | }) |
472bf1f5 JB |
47 | .add('Ternary with negation', function () { |
48 | nextWorkerIndex = 0 | |
0d6f0c13 | 49 | roundRobinTernaryWithNegation() |
472bf1f5 | 50 | }) |
e843b904 | 51 | .add('Ternary with pre-choosing', function () { |
aacd8188 | 52 | nextWorkerIndex = 0 |
0d6f0c13 | 53 | roundRobinTernaryWithPreChoosing() |
aacd8188 S |
54 | }) |
55 | .add('Increment+Modulo', function () { | |
56 | nextWorkerIndex = 0 | |
0d6f0c13 | 57 | roundRobinIncrementModulo() |
aacd8188 S |
58 | }) |
59 | .on('cycle', function (event) { | |
60 | console.log(event.target.toString()) | |
61 | }) | |
62 | .on('complete', function () { | |
63 | console.log( | |
64 | 'Fastest is ' + LIST_FORMATTER.format(this.filter('fastest').map('name')) | |
65 | ) | |
2f8c5b5c | 66 | // eslint-disable-next-line n/no-process-exit |
aacd8188 S |
67 | process.exit() |
68 | }) | |
69 | .run() |