

Difference between the Event Loop in Browser and Node Js?
source link: https://dev.to/jasmin/difference-between-the-event-loop-in-browser-and-node-js-1113
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Every JS developer must have heard of the term Event Loop. Both JS and Node Js is based on the principle of event loop which has similarities and dissimilarities to some extent. Let's discuss the event loop in brief and find the difference between them. 📖
Event Loop in Browser
Let's cover each section in brief here:
Heap - It stores all the object reference and variables that we define in our function.
Call Stack - All the function that we use in our code is stacked here in LIFO manner such that the last function is at the top and first function is at the bottom.
Web API's - These API's are provided by browser which provides additional functionality over V8 engine. The functions which uses these API's are pushed to this container which on completion of the response of Web API is popped out of this container.
-
Queues - The queues are used to compute the asynchronous code response such that it doesn't block engine to execute further.
- Macro Task Queue - This queue executes async functions like DOM events, Ajax calls and setTimeout and has lower priority than Job queue.
- Micro Task Queue - This queue executes async functions which uses promises and has higher precedence over Message Queue.
The event loop checks the Call Stack, if the stack is empty it pushes the functions in the Queues to Call Stack and runs it. Functions already present are given higher priority and runs first in comparison to functions in message queue.
Event Loop in Node Js
The Node Server consist of following parts:
Event Queue - On completion of the Thread Pool a callback function is issued and sent to the event queue. When call stack is empty the event goes through the event queue and sends callback to the call stack.
Thread Pool - The thread pool is composed of 4 threads which delegates operations that are too heavy for the event loop. I/O operations, Opening and closing connections, setTimeouts are the example of such operations.
Event loop in Node Js has different phases which has FIFO queue of callbacks to execute. When event loop enters a given phase it operates callbacks in that phase queue until the queue has been exhausted and maximum number of callbacks has executed and then moves to next phase.
The Event Loop is an endless loop which waits for the tasks, executes them and then sleeps until it receives more tasks. The event loop executes tasks from queue only when stack is empty. It processes the oldest task first and allows us to use callbacks and promises.
Difference between both the event loops?
First difference is node uses a thread pool to manage disk I/O. It executes the I/O and other timer API's asynchronously.
Browser does not have setImmediate() function. This function execute once the I/O operation is done, if particular code is inside this it will be executed first. Whereas in setTimeout() callback function is executed after given minimum threshold value in milliseconds.
Node Js event loop has multiple phases and each phase handle specific type of tasks whereas browser has micro task and macro task queue within which all the tasks are processed in order they were placed into the queue.
In a browser when you open a page in a tab, you actually create a process in which there can be multiple threads, such as JS engine, page rendering, HTTP request threads and many more. Whereas in Node JS you initiate a request, you actually create a thread that may be destroyed when the request is completed.
These are some major differences between the event loops for Node JS and Browser. Let me know if I missed something 😅
Happy Learning! 👩🏻💻
Recommend
-
9
Linux epoll & Node.js Event Loop & I / O复用select、poll、epoll都是I / O复用的机制,在《UNIX网络编程》里重点讲了select、poll的机制,但...
-
8
Understanding the Node.js Event LoopMay 28th 2021 6
-
5
Hello Everyone, The difference between for-of and for-in loop really troubled me when I was learning JavaScript. And with this blog I will try to clear the confusion once and for all. Let's understand them one by one.
-
7
Node.js Event Loop In ActionIn my last post(Deep dive into Node.js Architecture), we learnt about the internals of Node.js and how multiple asynchronous operatio...
-
2
原文地址:node事件循环(Event loop)本文将简述node事件循环的机制,帮助我们理解node环境...
-
9
Node.js Event Loop: Not So Single-ThreadedNode.js, Event Loop and Multi-ThreadingYou all know this or a similar sentence: Node.js is a single-threaded non-blocking...
-
7
JavaScript & Node事件循环机制(Event Loop)2020-01-18计算机原理作为一枚“前端打字员”,...
-
3
Master the Event Loop in Node.js
-
12
This blog post gives an overview of how Node.js works: What its architecture looks like. How its APIs are structured. A few highlights of its global variables and built-in modules. How it runs Ja...
-
5
An animated guide for Node.js event loop We have all heard about JavaScript and Node.js being single-threaded, but what does it mean in practical terms? It me...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK