Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Difference Between process.nextTick() and setImmediate() in Node.js

Tech May 15 1

Before diving into the difference, if you are using WebStorm as your Node.js IDE and notice that built‑in functions such as setImmediate() or require do not show autocompletion, you can fix this by enabling the Node.js core libray. Go to File → Settings → Languages & Frameworks → Node.js and NPM and set Node.js core Library is not enabled to Enable.

The Event Loop and Polling

Node.js is event‑driven. A continuous loop thread constantly picks tasks from the event queue and executes them, or hands off I/O operations to the background thread pool. Each iteration of this loop is considered one poll (or one tick).

setImmediate()

setImmediate() schedules a callback to be executed immediately after the current polling phase completes. To prevent blocking the poll phase, only one setImmediate callback is processed per loop iteration.

process.nextTick()

process.nextTick() schedules a callback to run before the next event loop iteration begins—i.e., after the current operaiton completes but before any I/O or timers. To avoid I/O starvation, the number of nextTick callbacks that can execute in a single loop is limited by process.maxTickDepth (default 1000).

Key Differences

  • process.nextTick() callbacks have higher priority than setImmediate() callbacks.
  • Internally, process.nextTick() belongs to the idle observer, while setImmediate() belongs to the check observer. During each cycle: idle observer runs before I/O observer, which runs before check observer.
  • process.nextTick() stores callbacks in an array and executes all of them in the current cycle. setImmediate() stores callbacks in a linked list and executes only one per cycle.

Code Example

// Register two nextTick callbacks
process.nextTick(function() {
    console.log("First nextTick");
});
process.nextTick(function() {
    console.log("Second nextTick");
    setImmediate(function() {
        console.log("Immediate inside nextTick");
    });
    process.nextTick(function() {
        console.log("Nested nextTick");
    });
});

// Register two setImmediate callbacks
setImmediate(function() {
    console.log("First setImmediate");
    process.nextTick(function() {
        console.log("NextTick inside first setImmediate");
    });
    setImmediate(function() {
        console.log("Inner setImmediate 1");
    });
});
setImmediate(function() {
    console.log("Second setImmediate");
    process.nextTick(function() {
        console.log("NextTick A inside second setImmediate");
    });
    process.nextTick(function() {
        console.log("NextTick B inside second setImmediate");
    });
    setImmediate(function() {
        console.log("Inner setImmediate 2");
    });
});

console.log("Synchronous log");

Output:

Synchronous log
First nextTick
Second nextTick
Nested nextTick
First setImmediate
Second setImmediate
Immediate inside nextTick
NextTick inside first setImmediate
NextTick A inside second setImmediate
NextTick B inside second setImmediate
Inner setImmediate 1
Inner setImmediate 2

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.