Asynchronous I/O

The main purpose and the initial idea behind Node.js was to be able to handle asynchronous I/O effectively. To achieve that goal, Node.js has a very good toolkit. It's built around libuv, which empowers the JavaScript language to do asynchronous I/O.

This means that it's kind of a silver bullet in this field. As long as your application is slim, not very CPU intensive, and is able to handle I/O efficiently, Node.js is the right tool for you.

Although it's true that your code runs in a single thread, as soon as your code needs to open a file or make an HTTP request, it uses other threads to do so. So, to really take advantage of the Node.js architecture, you should use it for writing code that actually needs the core API.

The simplified version of the Node.js Event Loop

The Event Loop is a loop mechanism that is responsible for handling asynchronous I/O code. The code you write synchronously runs immediately. The rest, like connecting to a third-party API, a database, or opening a file, will be queued in the poll. After that, if any timeout occurs (by a setTimeout or setInterval), they run. After that, run the I/O callbacks, if any. This is data from a file or a socket, for example. Finally, the close callbacks are executed. This is an over-simplified idea of the loop. There are several other tasks in-between (such as setImmediate before the close callbacks).

If you're writing code to perform some processor-intensive calculations, with big number manipulation or with fraction precision, Node.js will perform poorly. You may need to find modules to help you address these disadvantages by moving its weak points out of the JavaScript context.

If you still want to use Node.js for performance tasks, try creating a C++ module and use that instead. You can then pass the calculations to this module and still be able to use Node.js for other tasks that you would normally need more code for in C++.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset