DEV Community

Abel Terefe
Abel Terefe

Posted on

The main event: EVENT LOOPS

One thing javascript is well known for being asynchronous code. Meaning the code runs one line at a time and single - thread. However, when running javascript behavior on the browser, javascript codes are built on top of the browser.
Alt Text

Heaps are the mostly unstructured region of the memory. Think of it as a code that has not been run or placed on the stack. Stacks is the reader that reads each javascript language a thread at a time. When the stacks become slow, the term that is referred to that is called block barcode. Block barcode usually happens, when your code takes long-running one line on the stack. For example, iterating through an array that has a million people that has a chase bank account. Also, stacks keep track of where each code is on the file. This happens because it reads the function from top to bottom. Lastly, the stack is an array-like data structure with limitations like only additional items in the back and only remove the last item. You can think of it as first in first out, because

Web APIs or browsers are built-in browsers, it allows you to share data from the browser and other computer environment and allows you to do complex things with it. Web APIs are not javascript; however, javascript is built on top of web APIs that allow the user to have functionality when visiting the site. Web APIs have access like threads that allow you to make calls to this where the concurrency happens. When using a set timeout or any callback, the callback goes to the web API then the call back goes to the task queue then the event loop looks at the stacks and if the stack is empty the event queue takes the first thing in the past few and place it to the stack Event Loop has to wait for the stack to be clear before putting the call back in the stack. If there is a code on the stack you can't click on the screen and expect the response. the render runs after each callback queue. With this, all being said remember about how javascript runs on the web browser. Also, I highly recommend checking out Philp Roberts video https://youtu.be/8aGhZQkoFbQ. Mr. Roberts goes into great detail about this topic.
Alt Text

More resources:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop
https://hackernoon.com/understanding-js-the-event-loop-959beae3ac40

Top comments (0)