1 import Benchmark from 'benchmark'
3 import { LIST_FORMATTER } from '../benchmarks-utils.cjs'
5 function generateWorkersArray (numberOfWorkers) {
6 return [...Array(numberOfWorkers).keys()]
9 const workers = generateWorkersArray(60)
13 function roundRobinTernaryOffByOne () {
15 workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1
16 return workers[nextWorkerIndex]
19 function roundRobinTernaryWithNegation () {
21 !nextWorkerIndex || workers.length - 1 === nextWorkerIndex
24 return workers[nextWorkerIndex]
27 function roundRobinTernaryWithPreChoosing () {
28 const chosenWorker = workers[nextWorkerIndex]
30 workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1
34 function roundRobinIncrementModulo () {
35 const chosenWorker = workers[nextWorkerIndex]
37 nextWorkerIndex %= workers.length
41 new Benchmark.Suite('Round robin tasks distribution')
42 .add('Ternary off by one', () => {
44 roundRobinTernaryOffByOne()
46 .add('Ternary with negation', () => {
48 roundRobinTernaryWithNegation()
50 .add('Ternary with pre-choosing', () => {
52 roundRobinTernaryWithPreChoosing()
54 .add('Increment+Modulo', () => {
56 roundRobinIncrementModulo()
58 .on('cycle', event => {
59 console.info(event.target.toString())
61 .on('complete', function () {
63 'Fastest is ' + LIST_FORMATTER.format(this.filter('fastest').map('name'))