Whereas in Node JS when you initiate a Client Request which performs Blocking I/O operations, event loop picks up a thread and assign the client request to that thread as Event loop is single threaded. 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. 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. Whereas in setTimeout() callback function is executed after given minimum threshold value in milliseconds. This function execute once the I/O operation is done, if particular code is inside this it will be executed first. It executes the I/O and other timer API's asynchronously.īrowser does not have setImmediate() function. It processes the oldest task first and allows us to use callbacks and promises.įirst difference is node uses a thread pool to manage disk I/O. The event loop executes tasks from queue only when stack is empty. The Event Loop is an endless loop which waits for the tasks, executes them and then sleeps until it receives more tasks. 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. I/O operations, Opening and closing connections, setTimeouts are the example of such operations.Įvent loop in Node Js has different phases which has FIFO queue of callbacks to execute. Thread Pool - The thread pool is composed of 4 threads which delegates operations that are too heavy for the event loop. When call stack is empty the event goes through the event queue and sends callback to the call stack. The Node Server consist of following parts:Įvent Queue - On completion of the Thread Pool a callback function is issued and sent to the event queue. Functions already present are given higher priority and runs first in comparison to functions in 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. Micro Task Queue - This queue executes async functions which uses promises and has higher precedence over Message Queue.Macro Task Queue - This queue executes async functions like DOM events, Ajax calls and setTimeout and has lower priority than Job queue.Queues - The queues are used to compute the asynchronous code response such that it doesn't block engine to execute further. 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. Web API's - These API's are provided by browser which provides additional functionality over V8 engine. Heap - It stores all the object reference and variables that we define in our function.Ĭall 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. Let's discuss the event loop in brief and find the difference between them. Both JS and Node Js is based on the principle of event loop which has similarities and dissimilarities to some extent. Every JS developer must have heard of the term Event Loop.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |