Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

time-diff

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

time-diff - npm Package Compare versions

Comparing version
0.1.0
to
0.2.0
+233
README.md
# time-diff [![NPM version](https://img.shields.io/npm/v/time-diff.svg?style=flat)](https://www.npmjs.com/package/time-diff) [![NPM downloads](https://img.shields.io/npm/dm/time-diff.svg?style=flat)](https://npmjs.org/package/time-diff) [![Build Status](https://img.shields.io/travis/jonschlinkert/time-diff.svg?style=flat)](https://travis-ci.org/jonschlinkert/time-diff)
> Returns the formatted, high-resolution time difference between `start` and `end` times.
## Install
Install with [npm](https://www.npmjs.com/):
```sh
$ npm install time-diff --save
```
## Usage
Uses [pretty-time](https://github.com/jonschlinkert/pretty-time) to format time diffs.
```js
var Time = require('time-diff');
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
```
## API
### [.start](index.js#L57)
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**
```js
var time = new Time();
time.start('foo');
```
### [.end](index.js#L86)
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 diff
* `returns` **{Array}**: Returns the array from `process.hrtime()`
**Example**
```js
var time = new Time();
time.start('foo');
// do stuff
time.end('foo');
//=> 104μs
// do more stuff
time.end('foo');
//=> 1ms
// do more stuff
time.end('foo');
//=> 2ms
```
### [.diff](index.js#L134)
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:
<img width="559" alt="screen shot 2016-04-13 at 6 17 31 pm" src="https://cloud.githubusercontent.com/assets/383994/14512156/93f0bf78-01aa-11e6-9859-8f2a4b47043d.png">
**Params**
* `name` **{String}**: The name of the starting time to store.
* `options` **{String}**
**Example**
```js
var time = new Time();
var diff = time.diff('foo');
// do stuff
diff('foo');
//=> 104μs
// do more stuff
diff('bar');
//=> 1ms
// do more stuff
diff('baz');
//=> 2ms
```
## 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 show
* `options.digits`: the number of decimal places to display (`digits`)
**Examples**
_(See [pretty-time](https://github.com/jonschlinkert/pretty-time) for all available formats)_
Given the following:
```js
var time = new Time(options);
time.start('foo');
```
Returns milliseconds by default
```js
console.log(time.end('foo'));
//=> 13ms
```
Milliseconds to 3 decimal places
```js
console.log(time.end('foo', 'ms', 3));
// or
console.log(time.end('foo', 3));
//=> 12.743ms
```
Seconds to 3 decimal places
```js
console.log(time.end('foo', 's', 3));
//=> 0.013s
```
Seconds
```js
console.log(time.end('foo', 's'));
//=> 0s
```
Microseconds
```js
console.log(time.end('foo', 'μs'));
//=> 12ms 934μs
```
Microseconds to 2 decimal places
```js
console.log(time.end('foo', 'μs', 2));
//=> 14ms 435.78μs
```
nano-seconds
```js
console.log(time.end('foo', 'n', 3));
//=> 13ms 796μs 677ns
```
nano-seconds to 3 decimal places
```js
console.log(time.end('foo', 'n', 3));
//=> 13ms 427μs 633.000ns
```
## Related projects
You might also be interested in these projects:
* [ansi-colors](https://www.npmjs.com/package/ansi-colors): Collection of ansi colors and styles. | [homepage](https://github.com/doowb/ansi-colors)
* [log-utils](https://www.npmjs.com/package/log-utils): Basic utils for command line applications. | [homepage](https://github.com/jonschlinkert/log-utils)
* [pretty-time](https://www.npmjs.com/package/pretty-time): Easily format the time from node.js `process.hrtime`. Works with timescales ranging from weeks to nanoseconds. | [homepage](https://github.com/jonschlinkert/pretty-time)
* [time-diff](https://www.npmjs.com/package/time-diff): Returns the formatted, high-resolution time difference between `start` and `end` times. | [homepage](https://github.com/jonschlinkert/time-diff)
## Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/time-diff/issues/new).
## Building docs
Generate readme and API documentation with [verb](https://github.com/verbose/verb):
```sh
$ npm install verb && npm run docs
```
Or, if [verb](https://github.com/verbose/verb) is installed globally:
```sh
$ verb
```
## Running tests
Install dev dependencies:
```sh
$ npm install -d && npm test
```
## Author
**Jon Schlinkert**
* [github/jonschlinkert](https://github.com/jonschlinkert)
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
## License
Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert).
Released under the [MIT license](https://github.com/jonschlinkert/time-diff/blob/master/LICENSE).
***
_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on April 13, 2016._
+151
-5

@@ -10,5 +10,6 @@ /*!

var red = require('ansi-red');
var extend = require('extend-shallow');
var isNumber = require('is-number');
var pretty = require('pretty-time');
var isNumber = require('is-number');
var log = require('log-utils');

@@ -26,6 +27,11 @@ /**

function Time(smallest, digits) {
function Time(options) {
if (!(this instanceof Time)) {
return new Time(smallest, digits);
return new Time(options);
}
this.options = options || {};
var smallest = this.options.smallest;
var digits = this.options.digits;
if (isNumber(smallest)) {

@@ -35,2 +41,3 @@ digits = smallest;

}
this.smallest = smallest;

@@ -41,2 +48,14 @@ this.digits = digits;

/**
* Start a timer for the given `name`.
*
* ```js
* var time = new Time();
* time.start('foo');
* ```
* @param {String} `name` Name to use for the starting time.
* @return {Array} Returns the array from `process.hrtime()`
* @api public
*/
Time.prototype.start = function(name) {

@@ -46,6 +65,31 @@ return (this.times[name] = process.hrtime());

/**
* Returns the cumulative elapsed time since the **first time** `time.start(name)`
* was called.
*
* ```js
* var time = new Time();
* time.start('foo');
*
* // do stuff
* time.end('foo');
* //=> 104μs
*
* // do more stuff
* time.end('foo');
* //=> 1ms
*
* // do more stuff
* time.end('foo');
* //=> 2ms
* ```
* @param {String} `name` The name of the cached starting time to create the diff
* @return {Array} Returns the array from `process.hrtime()`
* @api public
*/
Time.prototype.end = function(name, smallest, digits) {
var start = this.times[name];
if (typeof start === 'undefined') {
throw new Error(red('start time not defined for "' + name + '"'));
throw new Error(log.colors.red('start time not defined for "' + name + '"'));
}

@@ -64,2 +108,101 @@

/**
* 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.
*
* ```js
* var time = new Time();
* var diff = time.diff('foo');
*
* // do stuff
* diff('foo');
* //=> 104μs
*
* // do more stuff
* diff('bar');
* //=> 1ms
*
* // do more stuff
* diff('baz');
* //=> 2ms
* ```
* Results in something like:
*
* <img width="559" alt="screen shot 2016-04-13 at 6 17 31 pm" src="https://cloud.githubusercontent.com/assets/383994/14512156/93f0bf78-01aa-11e6-9859-8f2a4b47043d.png">
*
* @param {String} `name` The name of the starting time to store.
* @param {String} `options`
* @api public
*/
Time.prototype.diff = function(name, options) {
var magenta = log.colors.magenta;
var gray = log.colors.gray;
var opts = {};
extend(opts, this.options, options);
if (typeof opts.times === 'undefined') {
return function() {};
}
this.start(name);
var time = this;
var prev;
function diff(msg) {
var val;
if (typeof prev !== 'undefined') {
val = time.end(prev);
}
if (typeof opts.diffColor === 'function') {
gray = opts.diffColor;
}
if (opts.color === false) {
magenta = identity;
gray = identity;
}
if (opts.times === true || opts.times === name) {
var timeDiff = magenta(time.end(name));
if (typeof val === 'string') {
timeDiff += gray(' (+' + val + ')');
}
// create the arguments to log out
var args = [name, msg, timeDiff];
// support custom `.format` function
if (typeof opts.format === 'function') {
opts.format.apply(null, args);
} else {
format.apply(null, args);
}
}
time.start(name);
prev = name;
};
return diff;
};
function identity(val) {
return val;
}
function format(name, msg, timeDiff) {
var args = [log.timestamp, name + ':', msg];
if (arguments.length === 3) {
args.push(timeDiff);
}
console.error.apply(console, args);
}
/**
* Expose `time`

@@ -69,1 +212,4 @@ */

module.exports = Time;
module.exports.format = format;
+1
-1
The MIT License (MIT)
Copyright (c) 2015, Jon Schlinkert.
Copyright (c) 2015-2016, Jon Schlinkert.

@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy

{
"name": "time-diff",
"description": "Returns the formatted, high-resolution time difference between `start` and `end` times.",
"version": "0.1.0",
"version": "0.2.0",
"homepage": "https://github.com/jonschlinkert/time-diff",

@@ -23,19 +23,27 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)",

"dependencies": {
"ansi-red": "^0.1.1",
"extend-shallow": "^2.0.1",
"is-number": "^2.1.0",
"log-utils": "^0.1.0",
"pretty-time": "^0.2.0"
},
"devDependencies": {
"mocha": "*"
"gulp-format-md": "^0.1.7",
"minimist": "^1.2.0",
"mocha": "^2.4.5",
"strip-color": "^0.1.0"
},
"keywords": [
"console",
"diff",
"difference",
"elapse",
"elapsed",
"log",
"time"
"pretty",
"terminal",
"time",
"time-diff",
"timer"
],
"verb": {
"related": {
"list": [
"pretty-time"
]
},
"plugins": [

@@ -45,6 +53,25 @@ "gulp-format-md"

"reflinks": [
"pretty-time"
"pretty-time",
"verb"
],
"layout": "default"
"related": {
"list": [
"log-utils",
"time-diff",
"pretty-time",
"ansi-colors"
]
},
"layout": "default",
"toc": {
"render": false
},
"run": true,
"lint": {
"reflinks": true
},
"tasks": [
"readme"
]
}
}
# time-diff [![NPM version](https://img.shields.io/npm/v/time-diff.svg)](https://www.npmjs.com/package/time-diff) [![Build Status](https://img.shields.io/travis/jonschlinkert/time-diff.svg)](https://travis-ci.org/jonschlinkert/time-diff)
> Returns the formatted, high-resolution time difference between `start` and `end` times.
- [Install](#install)
- [Usage](#usage)
- [API](#api)
- [Related projects](#related-projects)
- [Running tests](#running-tests)
- [Contributing](#contributing)
- [Author](#author)
- [License](#license)
_(TOC generated by [verb](https://github.com/verbose/verb))_
## Install
Install with [npm](https://www.npmjs.com/)
```sh
$ npm i time-diff --save
```
## Usage
Uses [pretty-time](https://github.com/jonschlinkert/pretty-time) to format time diffs.
```js
var Time = require('time-diff');
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
```
## API
Create an instance of `Time`, optionally specifying the time scale to use and the number of decimal places to display.
**Params**
* `smallest`: the smallest time scale to show
* `digits`: the number of decimal places to display (`digits`)
**Examples**
_(See [pretty-time](https://github.com/jonschlinkert/pretty-time) for all available formats)_
Given the following:
```js
var time = new Time();
time.start('foo');
```
Returns milliseconds by default
```js
console.log(time.end('foo'));
//=> 13ms
```
Milliseconds to 3 decimal places
```js
console.log(time.end('foo', 'ms', 3));
// or
console.log(time.end('foo', 3));
//=> 12.743ms
```
Seconds to 3 decimal places
```js
console.log(time.end('foo', 's', 3));
//=> 0.013s
```
Seconds
```js
console.log(time.end('foo', 's'));
//=> 0s
```
Microseconds
```js
console.log(time.end('foo', 'μs'));
//=> 12ms 934μs
```
Microseconds to 2 decimal places
```js
console.log(time.end('foo', 'μs', 2));
//=> 14ms 435.78μs
```
nano-seconds
```js
console.log(time.end('foo', 'n', 3));
//=> 13ms 796μs 677ns
```
nano-seconds to 3 decimal places
```js
console.log(time.end('foo', 'n', 3));
//=> 13ms 427μs 633.000ns
```
## Related projects
[pretty-time](https://www.npmjs.com/package/pretty-time): Easily format the time from node.js `process.hrtime`. Works with timescales ranging from weeks to nanoseconds. | [homepage](https://github.com/jonschlinkert/pretty-time)
## Running tests
Install dev dependencies:
```sh
$ npm i -d && npm test
```
## Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/time-diff/issues/new).
## Author
**Jon Schlinkert**
* [github/jonschlinkert](https://github.com/jonschlinkert)
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
## License
Copyright © 2015 [Jon Schlinkert](https://github.com/jonschlinkert)
Released under the MIT license.
***
_This file was generated by [verb](https://github.com/verbose/verb) on December 20, 2015._