What exactly is a Node.js event loop?

I got more information about the internals of Node.js architecture, and the term that I see is often found as a β€œtick”, as in the β€œnext tick of an event loop”, or as a function of nextTick () .

What I have not seen is a clear definition of what exactly is a tick. Based on various articles ( such as this one ), I was able to put together an idea in my head, but I'm not sure how accurate it is.

Can I get an accurate and detailed description of the checkmark for the Node.js event loop?

+63
javascript-events message-queue event-loop
Nov 06 '13 at 20:57
source share
3 answers

Remember that although JavaScript is single-threaded, all node I / O and calls to its own APIs are either asynchronous (using platform-specific mechanisms) or performed in a separate thread. (All of this is handled through libuv.)

Therefore, when the return data is available in a socket or in-built API function, we need a synchronized way to call a JavaScript function that is interested in the specific event that just happened.

It is unsafe to simply call the JS function from the thread in which the native event occurred for the same reasons that you encountered in a regular multi-threaded application - race conditions, non-atomic memory access, etc.

So what we do is put the event in the queue in a thread-safe manner. In a simplified psuedocode, something like:

lock (queue) { queue.push(event); } 

Then, back to the main JavaScript thread (but on the C side of things), we do something like:

 while (true) { // this is the beginning of a tick lock (queue) { var tickEvents = copy(queue); // copy the current queue items into thread-local memory queue.empty(); // ..and empty out the shared queue } for (var i = 0; i < tickEvents.length; i++) { InvokeJSFunction(tickEvents[i]); } // this the end of the tick } 

while (true) (which does not actually exist in the source code of the node, this is just illustrative) represents an event loop. The internal for calls the JS function for each event that was in the queue.

This is a checkmark: a synchronous call of zero or more callback functions associated with any external events. As soon as the queue is empty and the last function returns, the checkmark is completed. We go back to the beginning (next tick) and check for events that were added to the queue from other threads while our JavaScript was running.

What can add things to the queue?

  • process.nextTick
  • setTimeout / <T26>
  • I / O (material from fs , net , etc.)
  • crypto Processor functions like crypto streams, pbkdf2 and PRNG (which are actually an example ...)
  • any built-in modules that use the libuv work queue to make the synchronous calls to the C / C ++ library look asynchronous
+128
Nov 06 '13 at 21:52
source share

A simpler answer for beginners in JavaScript:

The first thing to understand is that JavaScript is a "single-threaded environment." This refers to the behavior of JavaScript when executing your code blocks in one of the "event loop" in a single thread. The following is an elementary implementation of the event loop, taken from the book by Kyle Simpson ydkJS, and then an explanation:

 // 'eventLoop' is an array that acts as a queue (first-in, first-out) var eventLoop = [ ]; var event; // keep going "forever" while (true) { // perform a "tick" if (eventLoop.length > 0) { // get the next event in the queue event = eventLoop.shift(); // now, execute the next event try { event(); } catch (err) { reportError(err); } } } 

The first while loop simulates an event loop. A tick is the removal of an event from the "event loop queue" and the execution of the specified event.

Please see the answer "Josh3796" for a more detailed explanation of what happens when you are removed from the queue and the event is executed.

I also recommend reading Kyle Simpson's book for anyone interested in deeply understanding JavaScript. It is completely free and open source and can be found at this link: https://github.com/getify/You-Dont-Know-JS

The specific section I referred to can be found here: https://github.com/getify/You-Dont-Know-JS/blob/master/async%20%26%20performance/ch1.md#event-loop

+6
Feb 18 '18 at 1:33
source share

A very simple and short way to tick Event Loop:

It is used by the internal mechanism of the node when, when processing multiple requests in a queue, a tick is launched that represents the completion of the task

0
Sep 06 '19 at 5:47
source share



All Articles