DannyNemer/counter

A profiling counter for Node.js.

★ 0Forks 0JavaScriptGitHub ↗Compare

README

counter

A profiling counter for Node.js.

Installation

npm install dannynemer/counter

Usage

var counter = require('counter')

for (var i = 0; i < 100; ++i) {
  if (i % 2 === 0) counter.count('even')
}

counter.end('even')
// => Resets the count for 'even' to 0
// => Prints "even: 50"

for (var i = 0; i < 99; ++i) {
  counter.count(i % 2 === 0 ? 'even' : 'odd')
  if (i > 100) counter.count('never reached')
}

counter.endAll()
// => Resets all counts to 0
// => Prints "even: 50 (50.5%)
//            odd: 50 (49.5%)"

counter

counter

counter.count(label)

# Ⓢ Ⓣ

Increments the invocation count for label. Use counter.end(label) or counter.endAll() to print the counter's value. This is useful to profile the number of times a section of code is reached.

Arguments

  1. label (string): The counter identifier.

Example

for (var i = 0; i < 100; ++i) {
  if (i % 2 === 0) counter.count('even')
}

counter.end('even')
// => Resets the count for 'even' to 0
// => Prints "even: 50"

counter.end(label)

# Ⓢ Ⓣ

Prints (and resets the value of) the number of counter.count(label) invocations.

Arguments

  1. label (string): The counter identifier.

counter.endAll

# Ⓢ Ⓣ

Prints (and resets the values of) the counter value of each label recorded by counter.count(), and each counter's value as a percentage of all counters.

Does not print counters that are never reached (having not initialized their keys). Prints counts in order of decreasing value.

Example

for (var i = 0; i < 99; ++i) {
  counter.count(i % 2 === 0 ? 'even' : 'odd')
}

counter.endAll()
// => Prints counts for all labels and resets all to 0

Output:
counter.endAll() example output


Contributors

DannyNemer

Issues