Event Loops are Your Friend

Philip Robert’s insightful explanation of Event Loops on youtube: https://www.youtube.com/watch?v=8aGhZQkoFbQ

I have never heard of Event Loops before this video. That makes sense since I’m just starting out in code, but I imagine that ELs are a black box to even intermediate coders, because you don’t really have to know how they do what they do to take advantage of their functionality, but it certainly helps!

Instead of working in a straight call stack, one task after another, you can ease up lag time between time-consuming functions by exploiting run timers, and pushing those tasks out of the stack and on to the webapi, where they will be count down until they are pushed to the EL. Here, they will wait until the stack is cleared, then they will be run.

I had to watch the video twice to understand it better, but for me, thinking of the stack, the webapi and the EL, reminded me of the way we structure our HTML, CSS, and javascript in the DOM. Javascript is kept at the bottom of the html, and saved for last, so that the html can be painted, and styled by CSS. This makes sense because the structural and visual elements are modified by JS, so putting JS first would slow everything down.

Following this analogy, functions and calls that require a wait time should be earmarked with run time, and pushed into a queue so that less arduous tasks can be addressed in the meantime while they process.

I’ve yet to utilize run time in any real world situations, but I can see conceptually how it will make things much more efficient for both the coder and the user.