Tech Talk 01: JavaScript Event Loop
Event Loop Credit: Rafsanjani

Tech Talk 01: JavaScript Event Loop


For those of us working in JavaScript for a while, we all know about the event loop mechanism. In this talk, we discuss how event loop works. So, without further due, lets get started.


Event Loop

The runtime memory model of JavaScript is called event loop, it is a constantly running process of the JavaScript that monitors the stack and event queue.

The JavaScript runtime model is a bit different from other languages because it is single-threaded.

It has three main components inside it:

1) Heap

2) Stack

3) Event Queue


Heap

Objects are stored in Heap memory. It is an unstructured place where newly created object or instances reside.


Stack

Every time a function call is encountered, that function is placed here. As stack performs in the LIFO (Last In First Out) manner, the latest function call is executed first than popped from the stack. If it encounters any blocking method call (API service call, timer etc.), that part is immediately moved to the browser Web API part.


Event Queue

An internal message queue for JavaScript.


Web API

This comes with web browser. Here blocking method calls are placed. First the method is executed here than passed to the event queue of JavaScript. Event queue is nothing but

a message queue. The message queue waits util the JavaScript runtime is emptied. Once the stack is empty the Event Loop deques the Event Queue and places the function in the stack.


Example:

Lets consider the following code:

function foo(){

   console.log("Start");
   
   setTimeout(function(){
    console.log("1 2 3...";)
   }, 2000);

  console.log("This is the end!");
}

foo();        


So, what is happening here? foo() is called. Inside the foo(), there are three statements. Lets see JavaScript runtime handles that.

JavaScript Runtime

In the initial stage, the heap is populated with objects. Then the stack is populated in the LIFO manner. It starts to execute the function calls one by one and pop them.


Web API


When it encounters the setTimeout(), it is immediately removed from Stack and placed to Web API. Then Stack is emptied. In the meantime, setTimeout() is also completed and is placed in the Event Queue.


Enqueue

The Event Loop monitors the stack and Event Queue continuously. If it finds anything in the queue then it checks whether Stack is empty or not. If empty, message is moved from Event Queue to the Stack.


Dequeue


In this manner, Stack continues to perform. Finally, Stack is cleared and program execution finishes.


Stack Cleared


In fine we can conclude that, Event Loop is noting but a background process which monitors and performs memory management.

要查看或添加评论,请登录

Sadat Rafsanjani的更多文章

  • Software Firm? Product-based or Service-based

    Software Firm? Product-based or Service-based

    So, without further due, lets get started. What it is? A software firm creates or innovates new ideas, products and…

  • Bloom Filter

    Bloom Filter

    What is it? A space efficient probabilistic (uses probability math) data structure. It is used to search element in a…

  • Linux File Permissions

    Linux File Permissions

    So without further due lets get started. What is it? File permission is a part of Linux administration where the system…

  • Tech Layoffs: You are Not Safe

    Tech Layoffs: You are Not Safe

    Tech layoffs are common these days. In fact experts are saying the chance of worsening is more likely to happen this…

  • Tech Talk 04: Integrating Google Translate in Angular

    Tech Talk 04: Integrating Google Translate in Angular

    So, without further due, lets get started. Introduction Google translate library is available to use for free.

  • Tech Talk 03: JavaScript Map vs. Filter vs. Reduce

    Tech Talk 03: JavaScript Map vs. Filter vs. Reduce

    So without further due, lets get started. Map A map is an array function that operates on an array.

  • Tech Talk 02: Two-Phase Commit (2PC)

    Tech Talk 02: Two-Phase Commit (2PC)

    So, without further due, lets get started! What is 2PC? Two-Phase Commit aka Tupac (2PC) is a protocol or distributed…

  • 5 Minutes Software Engineering: Engineering Best Practices & Principle

    5 Minutes Software Engineering: Engineering Best Practices & Principle

    KISS (Keep It Simple, Stupid) Try to keep your code simple and small as much as possible. Instead of deep nesting, lots…

  • Architect of Horror!-Java Virtual Machine

    Architect of Horror!-Java Virtual Machine

    Without further due, lets get started! What is JVM? Java virtual Machine (JVM) is an abstract machine. What is an…

  • Houston, We have a Problem! SSL Certificate Demystified

    Houston, We have a Problem! SSL Certificate Demystified

    Problem: How to provide a safe and secure environment for website? Why visitors should trust it? The Solution: Secure…

社区洞察

其他会员也浏览了