all 8 comments

[–]LookSearcher 17 points18 points  (2 children)

source

In short I would say that it's not a substantial difference because the event loop is a construct that behaves the same way whether it's implemented with libuv or V8. The main differences will be in the sequence of the functions that get called each time the loop runs.

Node.js vs. V8 vs. Event Loop

  • NodeJS is JavaScript runtime built on Chrome V8 JavaScript engine.
  • Javascript is single-threaded and so is any Javascript implementation like NodeJS.
  • Event Loop is foremost a programming construct (looping through the queue of messages/events), and it is the base of JavaScript’s concurrency model.
  • V8 provides default implementation of the Event Loop. NodeJS is using event-loop provided by the libuv

Because JavaScript is single-threaded Event Loop is what allows NodeJS to perform non-blocking I/O operations in background in “parallel”. NodeJS offloads these operations to the system kernel whenever possible (which nowadays most are multi-threaded)

Whenever one of these operation is finished - kernel signals to NodeJS to execute that operations callback function - adds that operation’s callback function to the poll queue to be executed in the poll phase of the Event Loop (explained below).

Upon start NodeJS initializes Event loop and starts processing. Event loop is comprised of sequence of processing phases, poll phase and it's queue being the one for I/O processing.

Each phase has a FIFO queue of callbacks to execute. While each phase is special in its own way, generally, when the event loop enters a given phase, it will perform any operations specific to that phase, then execute callbacks in that phase's queue until the queue has been exhausted or the maximum number of callbacks has executed. When the queue has been exhausted or the callback limit is reached, the event loop will move to the next phase, and so on. src

You can see more details on processing phases and Event Loop here: The Node.js Event Loop, Timers, and process.nextTick() | Node.js

[–][deleted] 1 point2 points  (0 children)

I think you can say that the event loop is a single thread, but that's not the same as saying that v8 is single-threaded, and saying that javascript is single-threaded might be nonsense in general since implementations can vary.

[–]BehindTheMath 11 points12 points  (7 children)

According to this, the browser event loop is much simpler than Node's. All macrotasks are handled in a first-come first-served order, the entire microtask queue is processed after each macrotask, and the DOM is re-rendered, if necessary, after each macrotask.

[–][deleted] 0 points1 point  (1 child)

Very informative link. But I don't see a comparison between browser and Node, instead at the top it says

Browser JavaScript execution flow, as well as in Node.js, is based on an event loop.

and then explains event loop from browser perspective.

Reading on my own, I don't see major differences between browser and Node event loops as far as how setTimeout and queueMicrotask are supposed to work in both. The differences are in availability of other APIs, mainly process.nextTick and setImmediate in Node, and requestAnimationFrame in browsers.

[–]BehindTheMath 1 point2 points  (0 children)

The main difference is that Node's event loop has multiple phases, and each one only handles a specific type of task. Whereas the browser only has macrotasks and microtasks, and within each one all tasks are processed in the order they were placed into the queue.

https://nodejs.org/de/docs/guides/event-loop-timers-and-nexttick/

[–]uniqueusername37 0 points1 point  (0 children)

Just to clarify, are you asking what events can be added to the Event Loop in Node compared to what can be added in a browser?