4

I am following the async module's each method (https://github.com/caolan/async#each). It says the method iterates over the array parallely. "Parallely" is the word that confuses me. AFAIK, in now way JavaScript can execute code parallely because it has a single-threaded model.

The examples shown in the each method focuses on the IO scenarios. I am using the "each" method just to add numbers of the array. If parallelism exists, can I prove this using my example?

Thanks for reading.

3
  • 2
    It's not parallel in terms of concurrency, but parallel in terms of independent from each other (see eachSeries for a version which processes each item after the other). For I/O, it makes sense; for calculations, it doesn't. Commented Mar 26, 2013 at 10:32
  • Thanks robertklep for the reply. Can you post your reply as a separate answer so that I can accept it? Commented Mar 26, 2013 at 16:33
  • I daresay the name is quite misleading. They should simply call it asynchronously, because that is what it actually does. The idea of "parallelism" conveys the meaning of operations that can overlap in time. This parallel function only guarantees that its function arguments will be invoked asynchronously. Commented Nov 27, 2013 at 17:18

2 Answers 2

3

The 'parallel' in the async documentation doesn't refer to 'parallel' in terms of concurrency (like multiple processes or threads being run at the same time), but 'parallel' in terms of each step being independent of the other steps (the opposite operation would be eachSeries, where each step is run only after the previous has finished).

The parallel version would only make sense if the steps perform some kind of I/O, which (because of Node's asynchronous nature) could run parallel to each other: if one step has to wait for I/O, the other steps can happily continue to send/receive data.

If the steps are mainly cpu-bound (that is, performing lots of calculations), it's not going to provide you any better performance because, like you say, Node runs the interpreter in a single thread, and that's not something that async changes.

Sign up to request clarification or add additional context in comments.

Comments

1

Like robertklep said, it is more of concurrent instead of parallel. You are not going to achieve much performance gain by doing compute heavy code in parallel. It is useful when you have to do parallel I/O (communicating with an external web service for all the items of an array, for example).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.