1.

JavaScript Memory Allocation and Event Loop

Answer»

In JavaScript, memory allocation is done in the following regions:


  • Heap memory: Data is stored in random order and memory is allocated accordingly.

  • Stack memory: Memory that is allocated in stacks. The majority of the time, it's employed for functions.

The function stack is a function that maintains track of all other functions that are running at the same time. An example to illustrate it is as follows:

function second() {
console.log("Second")
}
function First() {
second()
}
function foo() {
first()
}
foo()

The order in which functions are executed, that is. when they are popped out of the stack once their purpose is completed, is as follows:


  1. console.log

  2. second

  3. first

  4. foo


  • Event loop: An event loop is something that pulls various things like methods, etc. out of the queue and places it onto the function execution stack whenever the function stack becomes empty. The event loop is the trick to making JavaScript appear multithreaded even if it is only single-threaded. The following illusion clearly explains how the event loop works:

The callback function in the event queue has not yet started and is waiting for its time to be added to the stack when SetTimeOut() is called and the Web API waits. The function is loaded onto the stack when the function stack becomes empty, as seen below:

The event loop is used to take the first event from the Event Queue and place it on the stack, which in this case is the callback function. If this function is called from here, it will call other functions within it.




Discussion

No Comment Found