Does Node.js support parallelism?
If I'm not mistaken, you're looking for something along the lines of multithreading and/or computing multiple things at the same time.
Multiple executions at once
SIMD is beginning to find its way into Browsers, with Node implementations not far behind. Take a look at node-simd or MDN SIMD (browsers).
SIMD is still experimental, and only works on supported CPUs at the moment (obviously, since not all CPUs have this functionality). It executes multiple things at once, an example comparing normal JS to SIMD JS would be:
// Normal addition, 4 actions are executed.
var a = [1, 2, 3, 4];
var b = [5, 6, 7, 8];
var c = [];
c[0] = a[0] + b[0];
c[1] = a[1] + b[1];
c[2] = a[2] + b[2];
c[3] = a[3] + b[3];
c; // Array[6, 8, 10, 12]
// SIMD execution - all additions are processed simultaenously through the CPU
var a = SIMD.Float32x4(1, 2, 3, 4);
var b = SIMD.Float32x4(5, 6, 7, 8);
var c = SIMD.Float32x4.add(a,b);
c; // Float32x4[6, 8, 10, 12]
Multithreading
In regards to multithreading, webworkers
have existed in the Browser environment for a while now. This has been ported to Node fully according to the HTML spec as seen in this repository.
Here's a good explanation as to why webworkers
were even ported to Node in the first place, since you can already run child processes in other threads and on other CPUs with the child_process
module. node-webworker
is a great module since each process is run within its on context, and in its own node
process, so it is truly multithreading with node.
So, to run different processes on different CPUs, you would either adopt node-webworker
or use child_process
to spawn other threads to do separate actions at the same time with different CPU cores. node-webworker
can listen for events using the familiar postMessage
API, and child_process
will communicate through stdin / stdout / stderr
.
Hope that answers your question :)
Node can support "Parallelism" via either the Cluster
or child_process
modules packaged in the Nodejs Core API. Both of these modules create additional processes and not additional threads.
Threads are created and managed, sometimes in a pool, "under-the-hood" by libuv
which node
implements and uses to perform the asynchronous operations such as reading from a file. You cannot explicitly create a thread from your executing Javascript. You can read more about libuv
at the below resources
libuv
API Docslibuv
book- Threading in
libuv
(part oflibuv
book)
Also, this is great question on Threads and Pooling which has a lot of detail.
Cluster
is used distribute the workload of a Server across multiple cores and still have them share a port. However, Cluster
uses child_process.fork()
under the hood and communicates via inter-process communication. You can read more about Cluster
in the Nodejs Core API Docs.
child_process
offers a couple different ways to parallelize work via exec()
, spawn()
or fork()
. The parent process can communicate with the child process using inter-process communication via pipes.
Basically, Node wants you to leverage the event loop and leave thread management to libuv
. This is why it is so easy to build code that would typically require locking and thread safety measures. If you must do work that requires heavy lifting, or a significant amount of blocking (synchronous) work, then you could use child_process
to offload that work. If you need to scale out a web app across cores then use Cluster
.