Sam H
Sam H

Reputation: 561

What is the best and efficient way to do "multithreading" in node.js?

I'have been trying out the module "tiny-worker", but I feel that it's pretty slow to communicate between a worker thread and main thread using postMessage and onmessage, is this generally slow, because nodejs doesn't natively support multithreading? is there any other way to optimize the process? maybe using other modules?

Upvotes: 1

Views: 562

Answers (1)

Aniket Patel
Aniket Patel

Reputation: 61

I have tried a couple of modules out in npm and there is not very noticeable difference in performance for doing multi-threading in nodejs. Node.JS essentially spawns up child processes for doing multi-threading and all modules you use does the same thing in the background. Some modules you can try are 1. https://nodejs.org/api/worker_threads.html 2. https://nodejs.org/api/child_process.html 3. https://www.npmjs.com/package/worker-farm 4. https://www.npmjs.com/package/cluster 5. https://www.npmjs.com/package/tiny-worker (which you tried) and there are much more but I have tried this 5 only

I would recommend before going to multi-threading in nodejs please read about it's advantages and disadvantages and check if you really need it for your use case.

Upvotes: 1

Related Questions