0

I am trying to understand in which cases nodejs will be faster than its pros. I completely understood the terms Asynchronous I/O and non-blocking I/O but cant think of a use case where it is be useful. can somebody give me an example ?

0

2 Answers 2

3

Node is a prime example why async I/O is useful.

Node is (as far as the user is concerned) single threaded, so waiting for synchronous I/O would stop the only thread that is executing code. Since there are no guarantees how long I/O will take, that could/would make Node code run extremely slowly.

That's why Node pretty much uses only async I/O, it allows the single thread to quickly offload I/O work to the operating system while continuing code execution without interruption until the operating system notifies Node that the I/O operation is done.

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

3 Comments

sure but if the code that comes after the I/O depends purely on the I/O, then it has no advantage ?
@nik some people do think node.js has no advantage. Some think it's faster than threaded approach.
@nik Let's say user1 makes an access that requires accessing data in a slow database. While the database I/O is running, code is still running uninterrupted and other users can go on accessing the web server. If there is only one user, the advantage may be more limited, but if there are multiple users, they can all have I/O running at the same time.
0

NodeJS is basically a server side coding which is based on single threaded concept, so we have to manage all the I/O and CPU works on this thread itself.

We know, I/O operations are basic blocking operation for the running thread (example: I/O operation may include getting an input from a user, or reading a large file from the data center; this operations may hang up the thread for sometime, which may results in hanging up many client request).

To avoid this above cases, NodeJS came up with the concept of single thread asynchronous non-blocking I/O (also overcoming the overhead of creating multiple threads in case of multi-threading).

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.