1 import { bench, group, run } from 'tatami-ng'
3 function generateWorkersArray (numberOfWorkers) {
4 return [...Array(numberOfWorkers).keys()]
7 const workers = generateWorkersArray(60)
11 function roundRobinTernaryOffByOne () {
13 workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1
14 return workers[nextWorkerIndex]
17 function roundRobinTernaryWithNegation () {
19 !nextWorkerIndex || workers.length - 1 === nextWorkerIndex
22 return workers[nextWorkerIndex]
25 function roundRobinTernaryWithPreChoosing () {
26 const chosenWorker = workers[nextWorkerIndex]
28 workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1
32 function roundRobinIncrementModulo () {
33 const chosenWorker = workers[nextWorkerIndex]
35 nextWorkerIndex %= workers.length
39 group('Round robin tasks distribution', () => {
40 bench('Ternary off by one', () => {
42 roundRobinTernaryOffByOne()
44 bench('Ternary with negation', () => {
46 roundRobinTernaryWithNegation()
48 bench('Ternary with pre-choosing', () => {
50 roundRobinTernaryWithPreChoosing()
52 bench('Increment+Modulo', () => {
54 roundRobinIncrementModulo()
58 await run({ units: true })