Event loop
Event loop is a construction that is responsible for dispatching events in a program that almost always operates asynchronously with the message originator. When you call an I/O operation, NodeJS stores the callback assigned with that operation and continue processing other events. Callback will be triggered when all needed data is collected.
Here is more advanced definition of the event loop:
The event loop, message dispatcher, message loop, message pump, or run loop is a programming construct that waits for and dispatches events or messages in a program. It works by making a request to some internal or external “event provider” (which generally blocks the request until an event has arrived), and then it calls the relevant event handler(“dispatches the event”). The event-loop may be used in conjunction with a reactor, if the event provider follows the file interface, which can be selected or ‘polled’ (the Unix system call, not actual polling). The event loop almost always operates asynchronously with the message originator.
Here is a simple illustration that explains how event loop works in NodeJS.
NodeJS Event Loop
When a request is received by web-server it goes to the event loop. Event loop registers operation in a thread pool with assigned callback. Callback will be triggered when processing request is done. Your callback also can do other intensive operations like querying the database, but it does so the same way — registers operation in a thread pool with assigned callback and so on…
But what about code execution and its speed? Next, we are going to talk about virtual machine that executes JavaScript code — V8.
If you want to know more about how the v8 does with that loop you can read much more details about it in the follwoing article
The V8 and Lars Bak, the lead developer of V8: