The non-IO asynchronous API in Node.js provides four methods: setTimeOut(),setInterval(),setImmediate(), and process.nexttick (). All of these do similar things but achieve slightly different effects:
The Event Loop
First, we need to understand node.js’s event-based event model, which makes the callback function common in Node.js, and which enables efficient single-threaded asynchronous IO. The asynchronous IO part of Node.js is actually executed using thread pools).
In simple terms, when Node.js is started, a loop body similar to while(true) is created. Each loop body is called a tick. The process of each tick is to check whether there is an event waiting to be processed, if there is, the event and its related callback function is pulled out and executed, and the next tick is executed. So, there is code like this:
A();
B();
C();
Copy the code
The execution logic is to first ask the event observer whether there is a task that needs to be executed currently. The observer says “yes”, and executes A. Does A have A callback? If there is (if not, continue to ask if there is a task that needs to be executed), then the callback function is fetched and executed (note: The main function does not need to constantly ask for the result of the callback function. The main function does not need to constantly ask for the result of the callback function. The callback notifies the caller that I’m done (” Don’t call me, I will call you. “). Instead of waiting for the callback to complete, the main thread continues to execute, asking the observer again if there are any other tasks that need to be executed. Until the observer says no more, the thread ends.
The logic of the entire event loop is shown below:
SetTimeOut (),setInterval(),setImmediate(), and process.Nexttik ()
SetTimeOut () and setInterval() are similar except for the frequency, which means that the main thread executes immediately after a certain amount of time. SetImmediate () is similar, which means that the main thread executes immediately after it finishes. So what’s the difference between them?
The code is as follows:
setTimeout(function(){
console.log("setTimeout"); }, 0);setImmediate(function(){
console.log("setImmediate");
});
Copy the code
The setTimeout callback is mediate, and the setImmediate callback is mediate. The setTimeout callback is mediate, and the setImmediate callback is mediate. This is because they use different observers. SetTimeout uses an IO like observer, setImmediate uses a Check observer, and Process.nexttick () uses idle observers.
The priorities of the three types of observers are: Idle observer > IO observer > Check observer
The differences between process.nexttick () and setImmediate() and setTimeout() are as follows:
1. Original code:
A();
B();
C();
Copy the code
The order in which it is executed is code order:
A();
process.nextTick(B);
C();
Copy the code
It is executed in the following order:
A();
setImmediate(B); / / orsetTimeout(B,0);
C();
Copy the code
It is executed in the following order:
Conclusion: Process.nexttick () is the most efficient, consumes less resources, but blocks subsequent CPU calls; SetTimeout (), the accuracy is not high, the execution may be delayed, and because the red-black tree is used, so the resource consumption is high; SetImmediate (), which uses little resource and does not cause congestion, is also the least efficient.
Reprinted https://blog.csdn.net/hkh_1012/article/details/53453138