Home > Web Front-end > JS Tutorial > body text

Analysis of the difference between timer nextTick() and setImmediate() in node.js_node.js

WBOY
Release: 2016-05-16 16:30:02
Original
1769 people have browsed it

1. The problem with using timers in node is that it is not accurate. For example, setTimeout() sets a task to be executed after 10ms, but after 9ms, a task takes up 5ms, and when it is the timer's turn again , has been delayed by 4ms.

Okay, that’s all about the timer in node.

2. Look at the code:

Copy code The code is as follows:

process.nextTick(function(){
console.log("Delayed execution");
});
console.log("Normal execution 1");
console.log("Normal execution 2");
console.log("Normal execution 3");
console.log("Normal execution 4");

Through this example, I think everyone can clearly see what nextTick() is used for. It is mainly used for asynchronous execution.

Looking at the code:

Copy code The code is as follows:

setImmediate(function(){
console.log("Delayed execution");
});
console.log("Normal execution");

We found that setImmediate is also executed asynchronously. It’s strange

So what is the difference between it and nextTick()?

Look at the code:

Code 1:

Copy code The code is as follows:

process.nextTick(function(){
console.log("nextTick delay")
});
setImmediate(function(){
console.log("setImmediate delay");
});
console.log("Normal execution");

Result:

Code 2:

Copy code The code is as follows:

setImmediate(function(){
console.log("setImmediate delay");
});
process.nextTick(function(){
console.log("nextTick delay")
});
console.log("Normal execution");

Result:

I found that although the order of the codes is different, the execution result is the same.

It can be found from the results:

The execution priority of the callback function of nextTick() is higher than setImmediate();

process.nextTick() belongs to the idle observer, and setImmediate() belongs to the check observer. In each round of loop inspection, the idle observer precedes the I/O observer, and the I/O observer precedes the check observer. .

In terms of specific implementation, the callback function of process.nextTick() is stored in an array,
The result of setImmediate() is saved in the linked list.
In terms of behavior, process.nextTick() will execute all callback functions in the array in each cycle.
And setImmediate() executes a callback function in the linked list in each cycle.

Copy code The code is as follows:

//Add 2 callback functions for nextTick()
process.nextTick(function(){
console.log("nextTick delayed execution 1");
});
process.nextTick(function(){
console.log("nextTick delayed execution 2");
});
//Add two setImmediate() callback functions
setImmediate(function(){
console.log("setImmediate delayed execution 1");
Process.nextTick(function(){
console.log("Strong insertion");
});
});
setImmediate(function(){
console.log("setImmediate delayed execution 2");
});
console.log("Normal execution");

It can be seen from the execution results: when the first callback function of setImmediate() is executed, the second one is not executed immediately, but enters the next cycle. Press nextTick() again first, setImmediate( ) times. The reason for this design is to ensure that each loop can end quickly and prevent the CPU from occupying too much and blocking subsequent I/O calls.

The above is the information about the difference between the timer nextTick() and setImmediate() in node.js. Do you guys know the difference between them?

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!