Home > Web Front-end > JS Tutorial > Node.js Event Loop

Node.js Event Loop

Susan Sarandon
Release: 2024-12-14 01:19:14
Original
293 people have browsed it

Hello everyone,
In this first article, I will write about Event Loop (main loop, main thread, event thread, etc...), which is one of the topics that people who really want to understand Javascript think about. I would also like to add that this article will be a compilation of the notes I took for myself, not professionally. I apologize in advance for my mistakes.
In the image below, you see the javascript v8 engine, event loop mechanism and libuv library in Nodejs. It is the libuv library that performs some tasks that Javascript cannot provide. If we run javascript in the browser, we run things with the web api offered by the browsers.

Event loop is an architectural design pattern. The reason why this approach is preferred is directly related to the working principle of JavaScript. Javascript runs single threaded and Non-Blocking I/O. In other words, although it runs on a single thread, it is not blocked while executing time-consuming tasks and continues to work. It solves this with event loop architecture.

When the program runs, the Global Execution Context is created and added to the call stack. This is not removed from the call stack until the program flow is completed. In fact, an execution context is created for each function. Once the function is done, it is removed from the call stack. But it doesn't always work that way.

In some cases, the function takes time to complete its function. For example, a network request or a data read-write operation from a file, DOM events (not all). In this case, the function is processed by thread pools or Web APIs. When the process is finished, the callback function is added to the callback queue (task queue). When its turn comes (when the call stack is emptied), it is transferred to the call stack and processed. Once processed, it is removed from the call stack. Jobs in the microtask queue are processed before those in the callback queue. Examples of these are Promise, Mutation Observer, queueMicrotask

Thread pool in Node.js and Web Workers in browsers are used to execute asynchronous operations and jobs that require intensive CPU power. I will not touch upon their differences in usage and scope here. What I want to say is this: We make it possible to handle tasks that we cannot handle with JavaScript using different mechanisms.

Node.js Event Loop

The above is the detailed content of Node.js Event Loop. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template