Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท process

Node.js process.cpuUsage() and resourceUsage()

Reference for process.cpuUsage() and process.resourceUsage() in Node.js: user and system CPU time in microseconds, diffing two readings, and the extra counters.

process.cpuUsage() reports how much CPU time the current process has consumed, split into user time (your JavaScript and native code) and system time (the kernel working on behalf of the process). Both values are in microseconds and only ever grow.

process.resourceUsage() goes further and returns the operating system counters: peak memory, page faults, file system operations and context switches. Neither call tells you how long something took on the clock. For that, use process.hrtime().

NODE
const start = process.cpuUsage();
// ... do some work ...
const used = process.cpuUsage(start);

Try it

Node.js Measuring CPU time of a loop

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.

Node.js Waiting uses almost no CPU

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

NODE
process.cpuUsage([previousValue])
process.resourceUsage()
Name Description
previousValue Optional. An object returned by an earlier cpuUsage() call. The result is the difference since then.

Returns: cpuUsage() returns { user, system } in microseconds. resourceUsage() returns an object with these fields:

Field Meaning
userCPUTime, systemCPUTime CPU time in microseconds
maxRSS Peak resident memory, in kilobytes
sharedMemorySize, unsharedDataSize, unsharedStackSize Memory counters (often 0 on some systems)
minorPageFault, majorPageFault Page faults without and with disk access
swappedOut Swap counter (often 0)
fsRead, fsWrite File system read and write operations
ipcSent, ipcReceived, signalsCount Messages and signals
voluntaryContextSwitches, involuntaryContextSwitches Times the process gave up the CPU, or was forced to

Errors / edge cases: passing a previousValue that is not an object with numeric user and system properties throws a TypeError (code ERR_INVALID_ARG_TYPE). Negative or non-finite numbers are rejected with a RangeError (ERR_INVALID_ARG_VALUE).

Usage notes

  • Pass the old reading instead of subtracting by hand. process.cpuUsage(prev) does the subtraction for you and keeps the two fields together.
  • CPU time is summed over all threads. A process that keeps two cores busy for one second can report about two seconds of user time, so the value can be larger than the elapsed wall-clock time. Worker threads add to it.
  • Idle waiting is nearly free. Timers, sockets and file waits do not use CPU, so a low CPU figure next to a long wall-clock duration means "I/O bound".
  • Turn it into a percentage by dividing the CPU delta by the wall-clock delta between two samples, both in the same unit.
  • Resolution differs by platform. On some systems the counter advances in coarse steps, so very short loops can report 0.
  • maxRSS is in kilobytes, while memoryUsage() reports bytes. Do not mix them up.
  • Counters are cumulative from process start, not since your last call, unless you diff them yourself.