InterviewHubPRO
Section Overview
Q8

Question 8 of 60

Part 2: Runtime Internals, Libuv & The Event Loop (6 - 20)

13%
Node.js & Libuv•Part 2: Runtime Internals, Libuv & The Event Loop (6 - 20)

`process.nextTick()` vs `setImmediate()`.

In Simple Words:

  • process.nextTick(): Not technically part of the Event Loop phases; it drains its queue immediately after the current operation completes, before the Event Loop moves to any other phase. Starves the event loop if called recursively!
  • setImmediate(): Scheduled to execute specifically in the Check phase on the next tick of the Event Loop.
javascript
// Starving the event loop with recursive nextTick:
function recursiveTick() {
  process.nextTick(recursiveTick); // Blocks timers and I/O from ever executing!
}

// setImmediate allows other I/O to run in between:
function recursiveImmediate() {
  setImmediate(recursiveImmediate); // Does NOT block timers or I/O
}