ReasonJun

setTimeout, setImmediate, process.nextTick() 본문

Backend/Node.js

setTimeout, setImmediate, process.nextTick()

ReasonJun 2023. 9. 4. 00:59
728x90

setTimeout, setImmediate, and process.nextTick are all functions that allow you to schedule a callback function to be executed after a delay. However, they work in different ways and have different performance implications.

  • setTimeout(): setTimeout() schedules a callback function to be executed after a specified number of milliseconds. The callback function is placed on the event queue, and the event loop will call it after the specified delay.
  • setImmediate(): setImmediate() schedules a callback function to be executed immediately after the current event loop iteration. The callback function is placed on the check queue, and the event loop will call it after the current event loop iteration has completed.
  • process.nextTick(): process.nextTick() schedules a callback function to be executed immediately after the current event loop iteration, but before any other event handlers are called. The callback function is placed on the nextTick queue, and the event loop will call it before it calls any other event handlers in the current event loop iteration.

In general, setTimeout() is the most efficient way to schedule a callback function to be executed after a delay. However, it is important to note that the delay is not guaranteed, as it may be interrupted by other events.

setImmediate() is slightly less efficient than setTimeout(), but it is guaranteed to be executed immediately after the current event loop iteration. This makes it a good choice for scheduling callbacks that need to be executed quickly, such as logging or updating the UI.

 

process.nextTick() is the least efficient way to schedule a callback function to be executed after a delay. However, it is guaranteed to be executed before any other event handlers in the current event loop iteration. This makes it a good choice for scheduling callbacks that need to be executed before other events, such as handling a network request.

Here is a table that summarizes the differences between setTimeout(), setImmediate(), and process.nextTick():

 

Function Description Performance Use case
setTimeout() Schedules a callback function to be executed after a specified number of milliseconds. Efficient Scheduling callbacks that can be delayed.
setImmediate() Schedules a callback function to be executed immediately after the current event loop iteration. Less efficient than setTimeout() Scheduling callbacks that need to be executed quickly.
process.nextTick() Schedules a callback function to be executed immediately after the current event loop iteration, but before any other event handlers are called. Least efficient than setTimeout() and setImmediate() Scheduling callbacks that need to be executed before other events.
728x90

'Backend > Node.js' 카테고리의 다른 글

Node.js : How make asynchronous L/O (event loop)  (0) 2023.09.04
Single Thread / Multi Thread  (0) 2023.09.04
Node.js : Blocking / non-Blocking  (0) 2023.09.03
Node.js : bindings  (0) 2023.09.02
In Node.js, the relationship between V8 and libuv  (0) 2023.09.02
Comments