Skip to content

multithreading nodejs

Sᴛѧʀʟɪɴɢ edited this page Apr 23, 2019 · 8 revisions

he programming language goal is to support multiple (isolated) instances running on the same process without limiting capabilities of the framework since Google’s V8 engine already supports multiple and concurrent isolates. Every single isolate has its own heap memory and garbage collection.

JXcore official page says multiple isolates make JXcore more responsive and capable of handling expensive operations comparing to multi process models because of not having cross process communication and synchronization. Besides the performance gain, it also helps reaching beyond V8’s memory limitation. Every single thread has its own ‘separate’ heap memory space.


Threading at the JS level in node is really ineffective. V8 has a global lock on all JS objects that prevent them from being passed between threads. The only way to allow threading is to serialize and deserialize the objects into new "isolates" (V8 language). V8 does this for some pretty obvious reasons, you wouldn't really want to allow threading at the JS level in a browser, now would you.

You can use threads if you dive down into C++ land, but event then, you should have a really good reason for doing so, because if you're only manipulating objects for processing you're going to have to serialize into a C++ data-structure, process, and then serialize into a JS object. Threads in node are good for doing process intensive work that doesn't require any two way input from the main event loop, otherwise it's very unlikely to be worth your time or effort.

This is why fibers are kind of silly for node and is also why process intensive programs should most likely be avoided in node (with rare exception). Please use node for what it is good at; it's a short lived connection proxy maven, and good at ancillary front-end work that requires complex state transitions (i.e. mobile). Working it into a monolithic stack is just asking for trouble and pain. No serious architect should consider it for a large monolithic rendering stack.

https://medium.com/devschacht/a-cartoon-intro-to-arraybuffers-and-sharedarraybuffers-952198b0a1c9
http://aosabook.org/en/posa/from-socialcalc-to-ethercalc.html#multi-core-scaling
https://news.ycombinator.com/item?id=6834179
https://tproger.ru/translations/guide-to-threads-in-node-js/
https://itnext.io/multi-threading-and-multi-process-in-node-js-ffa5bb5cde98
https://medium.com/lazy-engineering/node-worker-threads-b57a32d84845
https://stackoverflow.com/questions/51053222/nodejs-worker-threads-shared-object-store
https://medium.com/@jsmrcaga/experimenting-with-nodes-worker-threads-a297f722b50d
https://habr.com/ru/company/ruvds/blog/437984/
https://stackoverflow.com/questions/25519325/share-objects-in-nodejs-between-different-instances
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics
https://github.com/tc39/ecmascript_sharedmem/blob/master/TUTORIAL.md
https://lucasfcosta.com/2017/04/30/JavaScript-From-Workers-to-Shared-Memory.html
https://stackoverflow.com/questions/48346490/what-does-the-atomics-object-do-in-javascript
https://www.alibabacloud.com/blog/improving-redis-performance-through-multi-thread-processing_594150
https://github.com/audreyt/node-webworker-threads

Clone this wiki locally