statman-stopwatch
is one of the metrics from the statman
library. It is a simple high res stopwatch for node.js. Stopwatch is useful for determining the amount of time it takes to perform an activity.
For example, you may want to determine how long certain potentially expensive activities take in your code (such as calling to an external web services or fetching a dataset from a database). Few lines of code will let you capture that info. There are much more elegant solutions - this is a simple roll-your-own approach.
New Features: {: .label .label-purple }
Install using npm:
npm install statman-stopwatch
Reference in your app:
const Stopwatch = require('statman-stopwatch');
const stopwatch = new Stopwatch();
statman
(deprecated)Install using npm:
npm install statman
Reference in your app:
const statman = require('statman');
const stopwatch = new statman.Stopwatch();
Stopwatch()
=> create instance of a stopwatchStopwatch(true)
=> create instance of stopwatch, and have it autostartStopwatch(name, autostart, delta)
=> create instance of stopwatch, with name, specify if to autostart, and supply an automatic delta (see setStartTimeDelta)start()
=> starts the stopwatch, let the timing begin!read(precision, units)
=> reads the stopwatch to determine how much time has elapsed. Note that the stopwatch continues to run. Returns the time elapsed in milliseconds.
precision
is provided, read()
will round to the number of decimals places based on precision. read
returns in ms
. If units
is specified to s
, will return values in seconds.time(precision)
=> alias for read()
stop()
=> stops the stopwatch, and returns the time elapsed in millisecondsrestart()
=> performs a stop()
and start()
split()
=> temp stops the stopwatch, allow read() to return time based on when split occurs. Use unsplit()
to resume the stopwatchunsplit()
=> use follow a split()
to resume the stopwatchsplitTime
=> while the stopwatch is split, returns the time as of the splitreset()
=> restores the stopwatch back to init state and clears start and stop timessetStartTimeDelta(number)
=> provide an elapsed time (in milliseconds) at which to start the stopwatchresume
=> used in conjunction with suspend
to pause/restart the stopwatchThere are some examples in example/example.js
Create a new stopwatch, start()
it, and later read()
it
const Stopwatch = require('statman-stopwatch');
const sw = new Stopwatch();
sw.start();
// do some activity
const delta = sw.read();
start()
is too hard. Create a new stopwatch with autostart=true, and later read()
it
const Stopwatch = require('statman-stopwatch');
const sw = new Stopwatch(true);
// do some activity
const delta = sw.read();
Create a new stopwatch, stop()
it, and later read()
it
const Stopwatch = require('statman-stopwatch');
const sw = new Stopwatch(true);
// do some activity
sw.stop();
// do some more activity
//returns time associated with when stop() occurred
const delta = sw.read();
There may be scenarios in which you need to add multiple timings together. To help with this, you can initialize the stopwatch with a value that will be added to the readings.
Note that most scenarios could also be achieved by suspending/resuming the stopwatch.
Create a new stopwatch, start()
it, and later read()
it
const Stopwatch = require('statman-stopwatch');
const sw = new Stopwatch();
sw.setStartTimeDelta(5000);
sw.start();
// do some activity which takes 500
const delta = sw.read();
// delta will be 5500 (the initial 5000ms set in setStartTimeDelta plus the elapsed 500ms)
There are times where you may want to exclude certain events from the stopwatch, so you can suspend
(pause) the stopwatch, then resume
after the excluded event is complete.
Create a new stopwatch, start()
it, and later read()
it
const Stopwatch = require('statman-stopwatch');
const sw = new Stopwatch();
sw.start();
// do some activity
sw.suspend();
//do some activity that should not be included in the timings
sw.resume();
let delta = sw.stop();
node
and npm
installednpm install
npm test