time-diff
![Build Status](https://img.shields.io/travis/jonschlinkert/time-diff.svg?style=flat)
Returns the formatted, high-resolution time difference between start
and end
times.
Install
Install with npm:
$ npm install time-diff --save
Usage
Uses pretty-time to format time diffs.
var Time = require('time-diff');
var time = new Time();
time.start('foo');
console.log(time.end('foo'));
API
Start a timer for the given name
.
Params
name
{String}: Name to use for the starting time.returns
{Array}: Returns the array from process.hrtime()
Example
var time = new Time();
time.start('foo');
Returns the cumulative elapsed time since the first time time.start(name)
was called.
Params
name
{String}: The name of the cached starting time to create the diffreturns
{Array}: Returns the array from process.hrtime()
Example
var time = new Time();
time.start('foo');
time.end('foo');
time.end('foo');
time.end('foo');
Returns a function for logging out out both the cumulative elapsed time since the first time .diff(name)
was called, as well as the incremental elapsed time since the last .diff(name)
was called. Unlike .end()
, this method logs to stderr
instead of returning a string. We could probably change this to return an object, feedback welcome.
Results in something like:
![screen shot 2016-04-13 at 7 45 12 pm](https://cloud.githubusercontent.com/assets/383994/14512800/478e1256-01b0-11e6-9e97-c6b625f097f7.png)
Params
name
{String}: The name of the starting time to store.options
{String}
Example
var time = new Time();
var diff = time.diff('my-app-name');
diff('after init');
diff('before options');
diff('after options');
Examples
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'));
Milliseconds to 3 decimal places
console.log(time.end('foo', 'ms', 3));
console.log(time.end('foo', 3));
Seconds to 3 decimal places
console.log(time.end('foo', 's', 3));
Seconds
console.log(time.end('foo', 's'));
Microseconds
console.log(time.end('foo', 'μs'));
Microseconds to 2 decimal places
console.log(time.end('foo', 'μs', 2));
nano-seconds
console.log(time.end('foo', 'n', 3));
nano-seconds to 3 decimal places
console.log(time.end('foo', 'n', 3));
Related projects
You might also be interested in these projects:
- ansi-colors: Collection of ansi colors and styles. | homepage
- log-utils: Basic utils for command line applications. | homepage
- pretty-time: Easily format the time from node.js
process.hrtime
. Works with timescales ranging from weeks to nanoseconds. | homepage - time-diff: Returns the formatted, high-resolution time difference between
start
and end
times. | homepage
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Building docs
Generate readme and API documentation with verb:
$ npm install verb && npm run docs
Or, if verb is installed globally:
$ verb
Running tests
Install dev dependencies:
$ npm install -d && npm test
Author
Jon Schlinkert
License
Copyright © 2016, Jon Schlinkert.
Released under the MIT license.
This file was generated by verb, v0.9.0, on April 13, 2016.