Skip to content

Instantly share code, notes, and snippets.

@0bie
Created June 26, 2017 02:41
Show Gist options
  • Save 0bie/f17581c0dac8b98dd25f1c0138f96257 to your computer and use it in GitHub Desktop.
Save 0bie/f17581c0dac8b98dd25f1c0138f96257 to your computer and use it in GitHub Desktop.
Understanding the event loop
// Passing a value of 0 to the `setTimeout` method deffers the callback to the end of the stack or until the stack is clear
setTimeout(function() {
console.log('test');
}, 0);
// `setTimoeout` is not a guaranteed time to exucution, it's a minimum time to execution
// Passing a value of 0 to `setTimeout` doesn't run the code immediately, it runs the code as soon as the stack is clear (nextish)
// The event loop pushes any deffered executions in the task/callback que only after the stack is clear
// The stack can continue to run while waiting for a request from the web API
// The browser can't perform a render while code is running on the stack
// Avoid putting slow/heavy duty code on the stack because it prevents browsers from rendering the UI
// Use asynchronous code to avoid blocking the event loop; when code gets into the callback que the browser can perform render
// https://www.youtube.com/watch?v=8aGhZQkoFbQ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment