Node.js Reference ยท node:sqlite and Runtime Services
Node.js performance.eventLoopUtilization() method
Reference for performance.eventLoopUtilization() in Node.js: idle and active time of the event loop, the utilization ratio, and measuring intervals with a baseline.
performance.eventLoopUtilization() reports how much of its life the event loop has spent idle (waiting for events) and active (running your callbacks). It returns an object with idle and active times in milliseconds and a utilization ratio between 0 and 1.
A utilization near 0 means the process is mostly waiting; near 1 means it is almost always busy and may be saturated. Unlike event loop delay, it tells you how much capacity is left. It is a cheap call, suitable for health checks and load shedding.
const elu = performance.eventLoopUtilization(); console.log(elu.utilization); // 0..1 since the loop started
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
performance.eventLoopUtilization(utilization1?: ELU, utilization2?: ELU): ELU // ELU = { idle: number, active: number, utilization: number }
| Name | Description |
|---|---|
utilization1 |
An earlier result. With one argument, the method returns the change between that reading and now. |
utilization2 |
A second earlier result. With two arguments, the method returns the difference between the two readings, without taking a new one. |
Returns: an object { idle, active, utilization }. idle and active are milliseconds; utilization is active / (idle + active).
Errors: none for valid objects. The values only make sense for results produced by this same method.
Usage notes
- Without arguments, the numbers are cumulative since the event loop started, so they are an average over the whole process life. Pass a previous reading to get a recent window.
- Typical monitoring pattern: keep the last reading, call the method on a timer with it, and use the returned
utilizationfor the last interval. Then store the fresh absolute reading for the next round. - Idle time is time spent waiting in the poll phase. Time inside your callbacks, including synchronous CPU work, counts as active.
- High utilization is not automatically bad, but sustained values near
1mean requests queue up. Pair it withmonitorEventLoopDelay()to see the lag it causes. - Garbage collection and native work done on the main thread count as active time.
- Before the loop starts, values are zero. Early in startup the numbers are small and unrepresentative.
- Per thread. A worker has its own
performanceobject with its own event loop utilization;worker.performance.eventLoopUtilization()reads it from outside. - Added in Node.js 14.10.0 and 12.19.0.