1 const Benchmark
= require('benchmark')
2 const { LIST_FORMATTER
} = require('./benchmark-utils')
4 const suite
= new Benchmark
.Suite()
6 function generateWorkersArray (numberOfWorkers
) {
7 return [...Array(numberOfWorkers
).keys()]
10 const workers
= generateWorkersArray(60)
14 function chooseWorkerTernaryOffByOne () {
16 workers
.length
- 1 === nextWorkerIndex
? 0 : nextWorkerIndex
+ 1
17 return workers
[nextWorkerIndex
]
20 function chooseWorkerTernaryWithNegation () {
22 !nextWorkerIndex
|| workers
.length
- 1 === nextWorkerIndex
25 return workers
[nextWorkerIndex
]
28 function chooseWorkerTernaryWithPreChoosing () {
29 const chosenWorker
= workers
[nextWorkerIndex
]
31 workers
.length
- 1 === nextWorkerIndex
? 0 : nextWorkerIndex
+ 1
35 function chooseWorkerIncrementModulo () {
36 const chosenWorker
= workers
[nextWorkerIndex
]
38 nextWorkerIndex
%= workers
.length
43 .add('Ternary off by one', function () {
45 chooseWorkerTernaryOffByOne()
47 .add('Ternary with negation', function () {
49 chooseWorkerTernaryWithNegation()
51 .add('Ternary with pre-choosing', function () {
53 chooseWorkerTernaryWithPreChoosing()
55 .add('Increment+Modulo', function () {
57 chooseWorkerIncrementModulo()
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 no-process-exit