Uses [pretty-time][] to format time diffs.
var Time = require('{%= name %}');
var time = new Time();
// create a start time for `foo`
time.start('foo');
// call `end` wherever the `foo` process ends
console.log(time.end('foo'));
//=> 12ms
{%= apidocs('index.js') %}
Disable time diffs, or filter time diffs to the specified name(s).
type: Boolean|String
default: undefined
Set to true
to disable color in the output.
type: Boolean
default: undefined
Example
var diff = time.diff('foo', {nocolor: true});
Format arguments passed to process.stderr
.
type: Function
default: undefined
Examples
Show message
and elapsed
time only:
var time = new Time();
var diff = time.diff('foo', {
formatArgs: function(timestamp, name, msg, elapsed) {
return [msg, elapsed];
}
});
diff('first diff');
//=> 'first diff 36μs'
diff('second diff');
//=> 'second diff 71μs'
Show name
and elapsed
time only:
var diff = time.diff('foo', {
formatArgs: function(timestamp, name, msg, elapsed) {
return [name, elapsed];
}
});
diff('first diff');
//=> 'foo 36μs'
diff('second diff');
//=> 'foo 71μs'
Create an instance of Time
, optionally specifying the time scale to use and the number of decimal places to display.
Options
options.smallest
: the smallest time scale to showoptions.digits
: the number of decimal places to display (digits
)
Examples
(See [pretty-time][] for all available formats)
Given the following:
var time = new Time(options);
time.start('foo');
Returns milliseconds by default
console.log(time.end('foo'));
//=> 13ms
Milliseconds to 3 decimal places
console.log(time.end('foo', 'ms', 3));
// or
console.log(time.end('foo', 3));
//=> 12.743ms
Seconds to 3 decimal places
console.log(time.end('foo', 's', 3));
//=> 0.013s
Seconds
console.log(time.end('foo', 's'));
//=> 0s
Microseconds
console.log(time.end('foo', 'μs'));
//=> 12ms 934μs
Microseconds to 2 decimal places
console.log(time.end('foo', 'μs', 2));
//=> 14ms 435.78μs
nano-seconds
console.log(time.end('foo', 'n', 3));
//=> 13ms 796μs 677ns
nano-seconds to 3 decimal places
console.log(time.end('foo', 'n', 3));
//=> 13ms 427μs 633.000ns
If you're using time-diff
with a command line application, try using [minimist][] for setting options.
var opts = {alias: {nocolor: 'n', logTime: 't', logDiff: 'd'}};
var argv = require('minimist')(process.argv.slice(2), opts);
var Time = require('time-diff');
var time = new Time(argv);