38e6fc7c5a4a772f50fb08f899173db7e3763b25
1 const Benchmark
= require('benchmark')
3 const suite
= new Benchmark
.Suite()
5 const LIST_FORMATTER
= new Intl
.ListFormat('en-US', {
10 const workers
= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
12 let nextWorkerIndex
= 0
14 function chooseWorkerTernary () {
16 workers
.length
- 1 === nextWorkerIndex
? 0 : nextWorkerIndex
+ 1
17 return workers
[nextWorkerIndex
]
20 function chooseWorkerIncrementModuloWithPreChoosing () {
21 const chosenWorker
= workers
[nextWorkerIndex
]
23 workers
.length
- 1 === nextWorkerIndex
? 0 : nextWorkerIndex
+ 1
27 function chooseWorkerIncrementModulo () {
28 const chosenWorker
= workers
[nextWorkerIndex
]
30 nextWorkerIndex
%= workers
.length
35 .add('Ternary', function () {
39 .add('Increment+Modulo with PreChoosing', function () {
41 chooseWorkerIncrementModuloWithPreChoosing()
43 .add('Increment+Modulo', function () {
45 chooseWorkerIncrementModulo()
47 .on('cycle', function (event
) {
48 console
.log(event
.target
.toString())
50 .on('complete', function () {
52 'Fastest is ' + LIST_FORMATTER
.format(this.filter('fastest').map('name'))
54 // eslint-disable-next-line no-process-exit