fe98eff1e0123ad143d2e394ffa59c53ee8afe4a
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 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', function () {
47 .add('Ternary with negation', function () {
49 chooseWorkerTernaryWithNegation()
51 .add('Ternary with PreChoosing', 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