Jiew Meng
Jiew Meng

Reputation: 88367

Is JavaScript/NodeJS truly parallel?

Recently in school, I've been taught C++/OpenMPI in a parallel computing class. I don't really like to program in C++ as its low level and harder to program, easier to make mistakes etc.

So I've been thinking, is JavaScript/NodeJS (something I've started to like) actually truly parallel? Or is it simply using non-blocking operations to simulate parallel execution (which I think it is)? There are libraries like async which gives similar functions to what I've used in OpenMPI: gather, scatter even "parallel". But I've got a feeling its just simulating parallelism using non-blocking IO?

Perhaps only node-webcl is truly parallel?

UPDATE: Seems possible via web workers (~31 min): watching http://www.infoq.com/presentations/Parallel-Programming-with-Nodejs

Upvotes: 1

Views: 869

Answers (2)

Viktor Molokostov
Viktor Molokostov

Reputation: 633

In deed, JavaScript is single-threaded by it's design. But you're not the first person who wants some parallelism in it, so there're some things that can work truly parallel:

  1. WebWorkers - run in threads, which means that they quite cheap to create. They limited in data interchange abilities. At first, you could only send messages between workers, but now they are a lot better, you can even use SharedArrayBuffer for concurrent memory access. Not supported in NodeJs, only in browsers.
  2. WebGL/WebCL - utilize graphic subsystems for parallel computing. Very fast, but effective for a limited set of problems. Not all tasks can be computed effectively on GPU-like subsystem. It also requires additional data transformations for presenting you data in pixel-like format. Has decent browsers support as WebGL, but as you've already mentioned has only experimental implementations for NodeJs.
  3. SIMD - parallelism over data. It was a promising thing, but it is no longer on the roadmap for JavaScript and it will be a part of the WebAssembly standard.
  4. Cluster - a NodeJs solution for parallelism. Allows to run multiple processes (not threads) and even supports SharedArrayBuffer for communication since 9th version.

That's pretty much it. There's also the WebAssembly threads proposal, but, firstly, it is a proposal and, secondly, WebAssembly is not JavaScript.

In general, JavaScript is by far not the best tool for low level parallel computing. There're a lot of other tools that suit better for this: Java, C#, Go...

Upvotes: 1

Michelle Tilley
Michelle Tilley

Reputation: 159145

With Node.js, your JavaScript runs in a single thread. IO is non blocking.

Upvotes: 0

Related Questions