1 import Benchmark from 'benchmark'
2 import { LIST_FORMATTER } from '../benchmarks-utils.mjs'
4 function generateWorkersArray (numberOfWorkers) {
5 return [...Array(numberOfWorkers).keys()]
8 const workers = generateWorkersArray(60)
12 function roundRobinTernaryOffByOne () {
14 workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1
15 return workers[nextWorkerIndex]
18 function roundRobinTernaryWithNegation () {
20 !nextWorkerIndex || workers.length - 1 === nextWorkerIndex
23 return workers[nextWorkerIndex]
26 function roundRobinTernaryWithPreChoosing () {
27 const chosenWorker = workers[nextWorkerIndex]
29 workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1
33 function roundRobinIncrementModulo () {
34 const chosenWorker = workers[nextWorkerIndex]
36 nextWorkerIndex %= workers.length
40 new Benchmark.Suite('Round robin tasks distribution')
41 .add('Ternary off by one', () => {
43 roundRobinTernaryOffByOne()
45 .add('Ternary with negation', () => {
47 roundRobinTernaryWithNegation()
49 .add('Ternary with pre-choosing', () => {
51 roundRobinTernaryWithPreChoosing()
53 .add('Increment+Modulo', () => {
55 roundRobinIncrementModulo()
57 .on('cycle', event => {
58 console.info(event.target.toString())
60 .on('complete', function () {
62 'Fastest is ' + LIST_FORMATTER.format(this.filter('fastest').map('name'))