What the heck is the event loop anyway? | Philip Roberts | JSConf EU

· algiegray's blog


Key Takeaways #

  1. JavaScript is single-threaded, meaning it can only execute one piece of code at a time This is different from languages that utilize threads.
  2. The JavaScript runtime (like V8) works in conjunction with Web APIs to enable asynchronous behavior. Web APIs are provided by the browser and allow for tasks like network requests and timers to be handled concurrently.
  3. The event loop is responsible for managing the communication between the JavaScript runtime and the callback queue. It continuously checks if the call stack is empty, and if so, takes the first item from the callback queue and pushes it onto the stack for execution.

Understanding the JavaScript Runtime #

The Role of the Event Loop #

Blocking Behavior #

Async Callbacks and the Event Loop in Action #

Examples of Asynchronous Behavior #

Don't Block the Event Loop! #

"The browser would like to repaint the screen every 16.6 milliseconds, 60 frame a second is ideal, that's the fastest it will do repaints if it can. But it's constrained by what you're doing in JavaScript for various reasons, so it can't actually do a render if there is code on the stack."

"Don't put shitty slow code on the stack because when you do that the browser can't do what it needs to do, create a nice fluid UI."

Summary for: Youtube