1 const Benchmark
= require('benchmark')
2 const { LIST_FORMATTER
} = require('../benchmarks-utils')
4 const suite
= new Benchmark
.Suite()
6 function generateWorkersArray (numberOfWorkers
) {
7 return [...Array(numberOfWorkers
).keys()]
10 const workers
= generateWorkersArray(60)
14 function roundRobinTernaryOffByOne () {
16 workers
.length
- 1 === nextWorkerIndex
? 0 : nextWorkerIndex
+ 1
17 return workers
[nextWorkerIndex
]
20 function roundRobinTernaryWithNegation () {
22 !nextWorkerIndex
|| workers
.length
- 1 === nextWorkerIndex
25 return workers
[nextWorkerIndex
]
28 function roundRobinTernaryWithPreChoosing () {
29 const chosenWorker
= workers
[nextWorkerIndex
]
31 workers
.length
- 1 === nextWorkerIndex
? 0 : nextWorkerIndex
+ 1
35 function roundRobinIncrementModulo () {
36 const chosenWorker
= workers
[nextWorkerIndex
]
38 nextWorkerIndex
%= workers
.length
43 .add('Ternary off by one', function () {
45 roundRobinTernaryOffByOne()
47 .add('Ternary with negation', function () {
49 roundRobinTernaryWithNegation()
51 .add('Ternary with pre-choosing', function () {
53 roundRobinTernaryWithPreChoosing()
55 .add('Increment+Modulo', function () {
57 roundRobinIncrementModulo()
59 .on('cycle', function (event
) {
60 console
.log(event
.target
.toString())
62 .on('complete', function () {
64 'Fastest is ' + LIST_FORMATTER
.format(this.filter('fastest').map('name'))
66 // eslint-disable-next-line n/no-process-exit