Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Globals and Console

Node.js console.count() method

Reference for console.count() and console.countReset() in Node.js: counting how often a code path runs, labels, resetting counters and typical uses.

console.count(label) keeps a counter per label. Every call increases that label's counter by one and prints label: N. Without an argument the label is default. console.countReset(label) sets the counter back to zero.

It is handy for answering "how many times does this function actually run?" without adding a variable.

NODE
console.count('render'); // render: 1
console.count('render'); // render: 2

Try it

Node.js Counting calls

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 Resetting counters

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
console.count([label = 'default']): void
console.countReset([label = 'default']): void
Name Description
label Counter name. Case-sensitive; converted to a string. Default 'default'.

Returns: undefined.

Edge cases: countReset() for a label that was never counted emits a process warning (Count for 'x' does not exist) and changes nothing. The counter has no upper limit besides number precision.

Usage notes

  • Counters live inside the console object, so they last for the whole program and survive across modules. Reset them in tests.
  • Output is always printed, there is no way to count silently. If you need the number, keep your own Map or variable.
  • Indentation from groups applies, so counts inside console.group() are indented.
  • Good uses: checking how often an event handler fires, spotting repeated renders, verifying a cache is hit. Remove the calls afterward; they write to stdout every time.
  • Labels can be anything printable, for instance console.count(user:${id}), but each distinct label stays in memory.
  • Per-console state: new Console(...) instances have their own counters, separate from the global console.
  • In production use metrics, not console counters; a counter library exposes numbers to dashboards rather than writing lines.