Node timer, nexttick () and setimmediate () Usage

Source: Internet
Author: User

1. the problem with using a timer in node is that it is not accurate. for example, setTimeout () sets a task to be executed after 10 ms, but after 9 ms, one task occupies 5 ms, and the timer has been delayed for 4 Ms again.

Well, the timer in the node can be described as much as this.

 

2. Check the Code:

1 process. nexttick (function () {2 console. log ("delayed execution"); 3}); 4 5 console. log ("normal execution 1"); 6 console. log ("normal execution 2"); 7 8 console. log ("normal execution 3"); 9 console. log ("normal execution 4 ");

Through this example, we can see clearly what nexttick () is for. It is mainly used for asynchronous execution.

Check the Code:

1 setimmediate (function () {2 console. Log ("delayed execution"); 3}); 4 console. Log ("normal execution ");

We found that setimmediate is also executed asynchronously. It's strange.

So what is the difference between it and nexttick?

Check the Code:

Code 1:

1 process. nexttick (function () {2 console. log ("nexttick latency") 3}); 4 5 setimmediate (function () {6 console. log ("setimmediate latency"); 7}); 8 9 console. log ("normal execution ");

Result:

Code 2:

1 setimmediate (function () {2 console. log ("setimmediate latency"); 3}); 4 process. nexttick (function () {5 console. log ("nexttick latency") 6}); 7 console. log ("normal execution ");

Result:

It is found that the execution results are the same although the code order is different.

 

From the results, we can find that:

The callback function execution priority of nexttick () is higher than that of setimmediate ();

Process. nexttick () belongs to the idle observer, and setimmediate () belongs to the check observer. In each round of loop check, the idle observer prevails over the I/O observer, and the I/O observer prevails over the check observer.

In specific implementation, the process. nexttick () callback function is saved in an array,
The result of setimmediate () is saved in the linked list.
In terms of behavior, process. nexttick () finishes executing all the callback functions in the array in each loop.
Setimmediate () executes a callback function in the linked list in each loop.

1 // Add two nexttick () callback functions 2 process. nexttick (function () {3 console. log ("nexttick delayed execution 1"); 4}); 5 process. nexttick (function () {6 console. log ("nexttick delayed execution 2"); 7}); 8 9 // Add two setimmediate () callback Functions 10 setimmediate (function () {11 console. log ("setimmediate delayed execution 1"); 12 process. nexttick (function () {13 console. log ("strong Insert"); 14}); 15}); 16 17 setimmediate (function () {18 console. log ("setimmediate delayed execution 2"); 19}); 20 21 console. log ("normal execution ");

The execution result shows that when the callback function of the first setimmediate () is executed, the second callback function is not executed immediately. Instead, the callback function enters the next round, the sequential execution after setimmediate. this design aims to ensure that the execution of each loop can be completed quickly. this prevents subsequent I/O calls from being blocked due to excessive CPU usage.

Node timer, nexttick () and setimmediate () Usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.