1 const Benchmark
= require('benny')
3 function generateWorkersArray (numberOfWorkers
) {
4 return [...Array(numberOfWorkers
).keys()]
7 const workers
= generateWorkersArray(60)
11 function roundRobinTernaryOffByOne () {
13 workers
.length
- 1 === nextWorkerIndex
? 0 : nextWorkerIndex
+ 1
14 return workers
[nextWorkerIndex
]
17 function roundRobinTernaryWithNegation () {
19 !nextWorkerIndex
|| workers
.length
- 1 === nextWorkerIndex
22 return workers
[nextWorkerIndex
]
25 function roundRobinTernaryWithPreChoosing () {
26 const chosenWorker
= workers
[nextWorkerIndex
]
28 workers
.length
- 1 === nextWorkerIndex
? 0 : nextWorkerIndex
+ 1
32 function roundRobinIncrementModulo () {
33 const chosenWorker
= workers
[nextWorkerIndex
]
35 nextWorkerIndex
%= workers
.length
40 'Round robin tasks distribution',
41 Benchmark
.add('Ternary off by one', () => {
43 roundRobinTernaryOffByOne()
45 Benchmark
.add('Ternary with negation', () => {
47 roundRobinTernaryWithNegation()
49 Benchmark
.add('Ternary with pre-choosing', () => {
51 roundRobinTernaryWithPreChoosing()
53 Benchmark
.add('Increment+Modulo', () => {
55 roundRobinIncrementModulo()