2

A Javascript web application has an event loop to handle the call stack and it loops around to check for any task queues returned by the browser's web API to run when the call stack is free. I will use v8 as the example for the javascript engine.

I've always pictured this call-stack as part of the V8 engine, it ultimately executes our Javascript as a synchronous program. Any delegation of multi-threaded action to an API like libuv/libevent must return back to the V8 execution call stack by asynchronous processing.

But recently, I've read chromium and node.js uses the event loop provided from libevent/libuv over the v8 implementation. This part really messes with my mental model a bit. The event loop is the perfect solution to get asynchronous processing when it resides in v8, we wait for API to return a queue to our macro/microtask and execute them by the next loop around priority check.

why do we need to take it out of v8 (as the most diagram shows event loop resides outside of v8)? if the event loop is outside of v8, does that mean v8 is no longer executing the javascript code anymore but instead the outside event loop executing our Javascript program and other codes provided by the respective libraries?

vasadia
  • 366
  • 5
  • 8
cozycoder
  • 309
  • 3
  • 9
  • Care to share the article you refer to? – hackape Aug 31 '21 at 23:25
  • 1
    @hackape it was answered here [link](https://stackoverflow.com/questions/31582672/what-is-the-different-between-javascript-event-loop-and-node-js-event-loop/61458912#61458912) by Sujeet Agrahari. – cozycoder Sep 01 '21 at 00:24
  • For browsers it's simply because they are not written in JavaScript. The big majority of what a browser does is not driven by JavaScript. Why would they rely on V8's small event-loop implementation? HTML's event-loop is far more complex. – Kaiido Sep 01 '21 at 01:44
  • @kaiido HTML's event-loop? Im aware that browsers provides many tools that operate on seperate threads for various specialized actions. but regardless in the perspective of the Javascript program we develop, shouldn't it only needs one simple event loop that waits and capture the result of those API back to our v8 call stack? – cozycoder Sep 01 '21 at 01:56
  • Yes, [HTML's event-loop](https://html.spec.whatwg.org/multipage/webappapis.html#event-loop-processing-model:event-loop). And no, once again the JS engine is almost nothing compared to the rest the browser implements. You can very well have a compliant browser that doesn't even execute JS at all. It would still need an event loop. – Kaiido Sep 01 '21 at 01:58
  • @Kaiido I see, so what you are saying is that the javascript program we write is only executing as a subset of the bigger runtime environment like the browser/node.js. because they are largely written in different langauge like C/C++. It makes mores sense for them to implement a event loop which speaks to the C/C++ than it is for all to to adapt to Javascript? – cozycoder Sep 01 '21 at 02:38
  • 2
    It's not really "an event-loop that talks to..." but rather an event-loop that is able to handle various task sources with prioritization, compound tasks, a rendering phase driven by the monitor refresh rate, and a lot of other stuff. So yes, they are better implementing their own even-loop rather than relying on the basic one coming in V8's box. – Kaiido Sep 01 '21 at 02:42

1 Answers1

2

Frankly this is also my first time knowing that event loop component is not part of v8.

But I don’t have issue understanding that we can take it out of v8. I know I’m not authoritative but here’s me sharing my understanding with you:

Your previous mind model goes like, v8 actively busy loops to check over signal of continuation.

In the new model, that responsibility is just shift over to libuv, v8 registers a callback to libuv, saying that “I’m waiting for signal of a certain file/network/timeout IO interrupt, call me back when it happens”, and then v8 goes to sleep, leaving libuv busy looping.

When that signal arrives, libuv callbacks to v8 to wake it up, and v8 continues to execute JS tasks. So not, libuv doesn’t execute JavaScript, it’s still v8.

The only thing changed is where to put the looping code. This is just a matter of where to draw the boundary between software components.

hackape
  • 18,643
  • 2
  • 29
  • 57