An online markdown blog and knowledge repository.
Notes taken while watching Philip Roberts' discussion on the Event Loop.
YouTube video What the heck is the event loop anyway?
Philip is a software developer who works for '&yet', a "dev-shop / product company" that specializes in real time help.
V8, single-threaded, callbacks, etc.
JS uses a synchronous language, but it has some asynchronous-esque capabilities.
v8 has a call stack and a heap, among other things.
V8 runs inside Chrome, that is the runtime for JS.
The browser provides some bigger picture items for the JS Runtime & V8:
On a regular basis the Event Loop does the following:
JS is single-threaded, therefore a single Call Stack.
Keeps the actively-executing function in memory while it executes.
'Main()' is a function in itself, indicating a JS file is being executed.
Every function-call within Main is then 'added to the stack'.
After each function "returns", it is removed from the Stack and the previous function entry continues excution.
When things are 'slow', it blocks execution of other methods, they have to 'wait'.
This is due to the synchronous behavior of the JS Runtime.
Single-threaded applications will suffer from this.
Browsers make this problem very apparent when blocking happens because the UI will not appear to respond, even to UI clicks or other events.
The browser has to wait until the call stack is cleared before waiting functions and requests can be executed.
JS - the Runtime - cannot make additional requests while it is working on something else.
The Browser has other APIs, that are basically threads that will do their own work.
The WebAPI cannot just push completed work onto the JS Stack so instead it puts the completed work item into the Task Queue.
Whenever the JS/V8 Stack is clear, the Event Loop takes the first-in work item from the Task Queue and passes it to the JS Stack to be executed.
Note: Node is like the DOM except instead of web APIs it is made of C++ APIs and hides all the concurrency and threading issues from the developer.
What about setting a function timeout timer to zero (set time zero) when we call a Web API? This forces the completed task to move directly to the Task Queue so it is 1st in line once the JS Stack is clear.
DOM Events, like 'onClick()' can be subject to asynchronous concurrency issues. For example:
The Browser checks to see if it can render the screen or handle any UI-related event triggers every few milliseconds.
When a function is sitting in the JS Call Stack, the Render Queue is blocked, which:
When you want to run code that is slow or potentially blocking:
"Don't block the event loop, else the browser cannot render."
Events from UI elements like the scroll bar can produce a massive number of events in a short period of time.