build(deps): bump poolifier
[poolifier.git] / benchmarks / worker-selection / round-robin.mjs
CommitLineData
98f60ddd 1import { bench, group, run } from 'tatami-ng'
aacd8188 2
292ad316
JB
3function generateWorkersArray (numberOfWorkers) {
4 return [...Array(numberOfWorkers).keys()]
5}
aacd8188 6
292ad316 7const workers = generateWorkersArray(60)
aacd8188 8
e843b904 9let nextWorkerIndex
aacd8188 10
0d6f0c13 11function roundRobinTernaryOffByOne () {
aacd8188
S
12 nextWorkerIndex =
13 workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1
14 return workers[nextWorkerIndex]
15}
16
0d6f0c13 17function roundRobinTernaryWithNegation () {
472bf1f5
JB
18 nextWorkerIndex =
19 !nextWorkerIndex || workers.length - 1 === nextWorkerIndex
20 ? 0
21 : nextWorkerIndex + 1
22 return workers[nextWorkerIndex]
23}
24
0d6f0c13 25function roundRobinTernaryWithPreChoosing () {
aacd8188
S
26 const chosenWorker = workers[nextWorkerIndex]
27 nextWorkerIndex =
28 workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1
29 return chosenWorker
30}
31
0d6f0c13 32function roundRobinIncrementModulo () {
aacd8188
S
33 const chosenWorker = workers[nextWorkerIndex]
34 nextWorkerIndex++
35 nextWorkerIndex %= workers.length
36 return chosenWorker
37}
38
0804b9b4
JB
39group('Round robin tasks distribution', () => {
40 bench('Ternary off by one', () => {
aacd8188 41 nextWorkerIndex = 0
0d6f0c13 42 roundRobinTernaryOffByOne()
f1c674cd 43 })
0804b9b4 44 bench('Ternary with negation', () => {
472bf1f5 45 nextWorkerIndex = 0
0d6f0c13 46 roundRobinTernaryWithNegation()
f1c674cd 47 })
0804b9b4 48 bench('Ternary with pre-choosing', () => {
aacd8188 49 nextWorkerIndex = 0
0d6f0c13 50 roundRobinTernaryWithPreChoosing()
f1c674cd 51 })
0804b9b4 52 bench('Increment+Modulo', () => {
aacd8188 53 nextWorkerIndex = 0
0d6f0c13 54 roundRobinIncrementModulo()
f1c674cd 55 })
0804b9b4
JB
56})
57
58await run({ units: true })