“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”

In a few words

This is the third time I learned the event cycle, and I got it this time. Understand NodeJS related principles and improve their level.

Today precipitate summary to learn the content

Event loop model

The JavaScript engine itself does not implement event loops. Event loops in the browser are implemented by the browser. Event loops in NodeJS are mainly implemented by the underlying Libuv library.

The Libuv library is event-driven, encapsulating and unifying API implementations across different platforms. NodeJS cross-platform and event loops are implemented through the Libuv library.

The above is divided into four steps:

NodeJS’s V8 engine parses the JavaScript code and then calls the Node API

2. NodeAPI assigns tasks to Libuv library.

3. Libuv manages the execution of these tasks through the event loop mechanism, and returns the results to the V8 engine after processing.

4, V8 engine callback processing Libuv return

Microtasks versus macro tasks

According to the type of tasks, different task queues, micro task queues and macro task queues.

Macro tasks: setTimeout, setInterval, and setImmediate

Microtasks: promise.then(), promise.catch(), process.nexttick ()

The stages of the event cycle

  • Timers: All setTimeout and setInterval callbacks are executed
  • Pendding Callback phase: A callback for certain system operations. For example, TCP connection errors
  • Poll phase: Polling waits for events such as new links and requests and performs I/O callbacks. For example, file reading operations
  • Check phase: The callback to setImmedidate is executed
  • Close callback phase: closes callback execution, such as socket.on(close…)

Each phase will execute the task queue of the current phase, and then execute the microtask queue of the current phase. Only when all microtasks are executed can the next phase be carried out.

Event loop difference in browser:

There are no stages in the browser; Priority: Sync code, microtask, macro task

In the browser, there may be multiple queues for macro tasks, but only one microtask, and each time the microtask is executed before the macro task is executed

Statement to summarize

Main learning, NodeJS event loop model, parsing JS code from V8 engine, and then call NodeAPI, NodeAPI will assign the task to the underlying Libuv library to execute, the main event loop mechanism is completed by Libuv library.

The whole event loop is divided into six phases: timer phase, Pendding callback phase, poll phase, check phase, and close callback phase. The execution sequence is as follows: The microtask is executed after the task queue in each phase is executed, and the next phase is executed only after the execution of the microtask is completed. The browser’s event loop is unstaged, prioritized: synchronous code, microtask, macro task.

The interview questions

Question 1: What is your understanding of NodeJS event loops?

Node is single-threaded. The main thread puts all tasks in the loop queue, and then the underlying Libuv library takes tasks from the loop event queue and assigns them to different threads for processing. The main thread also carries out callback processing at the same time, forming an event loop.

Difference with browser event loop:

The browser is not divided into multiple stages

2. Browser task priority: sync code, microtask, macro task

Question 2: The execution order of tasks in the event queue?

The NodeJS event loop is divided into multiple stages, and the execution sequence is: after the execution of one phase, the microtask is executed, and then the next phase is executed.