perf: use a single map to store pool workers and their related data
[poolifier.git] / src / pools / selection-strategies / round-robin-worker-choice-strategy.ts
index 76cdd4abac3ff7a2c5a5988cf76e66d87ab9be9b..44dfa4b76a941fe6caa005fe07bf5dc5ccd34644 100644 (file)
@@ -14,23 +14,24 @@ export class RoundRobinWorkerChoiceStrategy<
   Response
 > extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
   /**
-   * Index for the next worker.
+   * Id of the next worker.
    */
-  private nextWorkerIndex: number = 0
+  private nextWorkerId: number = 0
 
   /** {@inheritDoc} */
   public reset (): boolean {
-    this.nextWorkerIndex = 0
+    this.nextWorkerId = 0
     return true
   }
 
   /** {@inheritDoc} */
   public choose (): Worker {
-    const chosenWorker = this.pool.workers[this.nextWorkerIndex]
-    this.nextWorkerIndex =
-      this.nextWorkerIndex === this.pool.workers.length - 1
+    const chosenWorker = this.pool.workers.get(this.nextWorkerId)
+      ?.worker as Worker
+    this.nextWorkerId =
+      this.nextWorkerId === this.pool.workers.size - 1
         ? 0
-        : this.nextWorkerIndex + 1
+        : this.nextWorkerId + 1
     return chosenWorker
   }
 }