Revert choose worker implementation (#148)
authorShinigami <chrissi92@hotmail.de>
Sun, 14 Feb 2021 07:12:25 +0000 (08:12 +0100)
committerGitHub <noreply@github.com>
Sun, 14 Feb 2021 07:12:25 +0000 (08:12 +0100)
benchmarks/choose-worker.js [new file with mode: 0644]
src/pools/abstract-pool.ts

diff --git a/benchmarks/choose-worker.js b/benchmarks/choose-worker.js
new file mode 100644 (file)
index 0000000..38e6fc7
--- /dev/null
@@ -0,0 +1,57 @@
+const Benchmark = require('benchmark')
+
+const suite = new Benchmark.Suite()
+
+const LIST_FORMATTER = new Intl.ListFormat('en-US', {
+  style: 'long',
+  type: 'conjunction'
+})
+
+const workers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
+
+let nextWorkerIndex = 0
+
+function chooseWorkerTernary () {
+  nextWorkerIndex =
+    workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1
+  return workers[nextWorkerIndex]
+}
+
+function chooseWorkerIncrementModuloWithPreChoosing () {
+  const chosenWorker = workers[nextWorkerIndex]
+  nextWorkerIndex =
+    workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1
+  return chosenWorker
+}
+
+function chooseWorkerIncrementModulo () {
+  const chosenWorker = workers[nextWorkerIndex]
+  nextWorkerIndex++
+  nextWorkerIndex %= workers.length
+  return chosenWorker
+}
+
+suite
+  .add('Ternary', function () {
+    nextWorkerIndex = 0
+    chooseWorkerTernary()
+  })
+  .add('Increment+Modulo with PreChoosing', function () {
+    nextWorkerIndex = 0
+    chooseWorkerIncrementModuloWithPreChoosing()
+  })
+  .add('Increment+Modulo', function () {
+    nextWorkerIndex = 0
+    chooseWorkerIncrementModulo()
+  })
+  .on('cycle', function (event) {
+    console.log(event.target.toString())
+  })
+  .on('complete', function () {
+    console.log(
+      'Fastest is ' + LIST_FORMATTER.format(this.filter('fastest').map('name'))
+    )
+    // eslint-disable-next-line no-process-exit
+    process.exit()
+  })
+  .run()
index f6ed280cc3bbcf39b6a4aed1faa37603bf17786d..ac3bd6cc7b02f0f7e592de747a5668a25d845f09 100644 (file)
@@ -220,8 +220,10 @@ export abstract class AbstractPool<
    */
   protected chooseWorker (): Worker {
     const chosenWorker = this.workers[this.nextWorkerIndex]
-    this.nextWorkerIndex++
-    this.nextWorkerIndex %= this.workers.length
+    this.nextWorkerIndex =
+      this.workers.length - 1 === this.nextWorkerIndex
+        ? 0
+        : this.nextWorkerIndex + 1
     return chosenWorker
   }