Zanko
Zanko

Reputation: 4694

Should we avoid normal for-loop in NodeJS at all time to improve concurrency?

Lets say I have a really simple demo function in one of a route that does

let array = [];
for (var i = 0; i < 1000; i++) {
   array[i] = i * 10;
}

this is going to be a really fast loop. But will it be overkill to turn this into asynchronous loop using async.each(...).

Should I avoid normal for-loop at all time and try to convert all the loop to asynchronous?

That being said, I have been using underscore.each alot in my project and now I am planning to covert all of it to async.each no matter how simple the loop is.

Upvotes: 1

Views: 1101

Answers (4)

jfriend00
jfriend00

Reputation: 707876

It would be overkill to convert all for loops to asynchronous operations.

One rule I follow in programming is to write code as simply as possible and add complication only as needed to solve problems you have a proven need for and even then, only when you couldn't find another simple way to solve the problem. So, never spend time and code trying to optimize things that you haven't analyzed enough to know you actually have a problem that needs solving.

So, in your specific case, you need to discover that you have a particular for loop that is taking too long as is thus messing with the responsiveness of your server and ONLY work on that particular for loop.

Proving this would involve:

  1. Recognizing that you have an actual server responsiveness issue. No need to add code to solve a problem you don't actually have.

  2. Profiling your code to find out where the bottlenecks actually are. They are often not what you might think so you have to measure to get data on where the bottlenecks are.

  3. Deciding architecturally what the best way is to deal with those bottlenecks. For example, you might just be better off using node.js clustering with 4 processes on a 4 CPU system. Or, you might be better off implementing a custom process and a work queue to process the CPU intensive tasks in separate processes. It all depends upon the specifics of the work to be done - there is no single generic answer for how to scale or handle load.

Upvotes: 2

Paul
Paul

Reputation: 141887

Should I avoid normal for-loop at all time and try to convert all the loop to asynchronous?

No, that is not necessary if the whole loop will complete quickly.

It's a bit like asking if you should wrap every statement with setTimeout( function ( ) { // statement }, 0 ); to give the event-loop a chance to handle I/O .

If you want to allow I/O to be handled while looping, the best thing you can do is run multiple processes, so that the operating system can handle the scheduling and use multiple cores when possible.

Upvotes: 1

schu34
schu34

Reputation: 985

While I don't think it would hurt to use async.each for all your loops, it's probably not necessary unless the operation inside the for loop is going to take a long time at each iteration. In the example, multiplying a number and inserting it into an array is going to take almost no time, so rewriting it using async.each probably wouldn't effect your performance too much.

Upvotes: 1

salezica
salezica

Reputation: 77039

In low-level pseudocode, a synchronous loop looks like this:

  • Check stop condition
  • Run inner code
  • Repeat

The asynchronous version:

  • Check stop condition
  • Run inner code
  • Yield control to other tasks
  • Wait for control to be yielded back to us
  • Repeat

If you're performing a super-intensive computational task, and fear that during the time your loop is running other tasks may be blocked, it may make sense to yield control occasionally.

If running the loop is the only, or most important thing your program is doing; or if the entire iteration takes little time, you're only adding overhead and reducing performance.

The example you gave falls squarely into the 2nd category. Your loop is simple and the whole iteration takes little time. You can't possibly be blocking other tasks from running in your Node process. There's no reason to yield control during the loop, much less in every iteration. You will only make the process take longer.

Use asynchronous tools only if you need to handle several tasks at the same time, and your otherwise sync loop is blocking the rest from running. This is usually not the case. Make it the exception.

Upvotes: 1

Related Questions