Soorya Prakash
Soorya Prakash

Reputation: 991

Performance impact during the multiple callback functions after sending response

Can anyone help me understand the function of NodeJS and performance impact for the below scenario.

a. Making the request to Rest API end point "/api/XXX". In this request, i am returning the response triggering the asynchronous function like below.

 function update(req, res) {
        executeUpdate(req.body); //Asynchronous function
        res.send(200);
 }

b. In this, I send the response back without waiting for the function to complete and this function executing four mongodb updates of different collection.

Questions:

Upvotes: 1

Views: 369

Answers (2)

twg
twg

Reputation: 1105

There are, I think, two different parts in the answer to your question.

  1. Actual Performance - which includes CPU & memory performance. It also obviously includes speed.
  2. Understanding as the previous poster said, Sync and Async.

In dealing with #1 - actual performance the real only way to test it is to create or use a testing environment on your code. In a rudimentary way based upon the system you are using you can view some of the information in top (linux) or Glances will give you a basic idea of performance, but in order to know exactly what is going on you will need to apply some of the various testing environments or writing your own tests.

Approaching #2 - It is not only sync and async processes you have to understand, but also the ramifications of both. This includes the use of callbacks and promises. It really all depends on the current process you are attempting to code. For instance, many Node programmers seem to prefer using promises when they make calls to MongoDB, especially when one requires more than one call based upon the return of the cursor.

There is really no written-in-stone formula for when you use sync or async processes. Avoiding callback hell is something all Node programmers try to do. Catching errors etc. is something you always need to be careful about. As I said some programmers will always opt for Promises or Async when dealing with returns of data. The famous Async library coupled with Bluebird are the choice of many for certain scenarios.

All that being said, and remember your question is general and therefore so is my answer, in order to properly know the implications on your performance, in memory, cpu and speed as well as in return of information or passing to the browser, it is a good idea to understand as best as you can sync, async, callbacks, promises and error catching. You will discover certain situations are great for sync (and much faster), while others do require async and/or promises.

Hope this helps somewhat.

Upvotes: 1

Sachin
Sachin

Reputation: 3540

  • In short, it depends on what you are doing in your function.

  • The synchronous functions in node are executed on main thread, thus, they will not preempt and execute until end of the function or until return statement is encountered.

  • The async functions, on the other hand, are removed from main thread, and will only be executed when async tasks are completed on a
    separate worker thread.

Upvotes: 1

Related Questions