Created
April 10, 2021 17:12
-
-
Save GaetanoPiazzolla/8308bfbcba7c9a5bc1879758e9cdc801 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let numberOfThreads = 10; | |
const workerArray = []; | |
for (let n = 0; n < numberOfThreads; n++) { | |
workerArray.push(new Worker("./project_modules/worker.js")) | |
} | |
let i = 0; | |
const worker_array_calculus = (num,threads) => { | |
return new Promise((resolve, reject) => { | |
let workerNum = i % threads; | |
console.log('worker num:', workerNum) | |
workerArray[workerNum].on("message", result => { | |
resolve(result.output) | |
}); | |
workerArray[workerNum].postMessage({num: num}); | |
i++; | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related to medium article: https://gae-piaz.medium.com/achieve-the-best-performance-10x-faster-node-js-with-worker-threads-97fc2890e40f