Comparing version 0.1.1 to 0.2.0
21
index.js
@@ -0,11 +1,28 @@ | ||
var S = 'seconds'; | ||
var MS = 'milliseconds'; | ||
var NS = 'nanoseconds'; | ||
function hirestime() { | ||
var start = process.hrtime(); | ||
return function() { | ||
return function(unit) { | ||
var elapsed = process.hrtime(start); | ||
return elapsed[0] * 1e3 + elapsed[1] / 1e6; | ||
return recalc(elapsed[0] * 1e3 + elapsed[1] / 1e6, unit); | ||
} | ||
} | ||
function recalc(msTime, unit) { | ||
var time; | ||
if(!unit || unit === MS) time = msTime; | ||
if(unit === S) time = msTime / 1000; | ||
if(unit === NS) time = msTime * 1000; | ||
return parseInt(time * 100, 10) / 100; | ||
} | ||
hirestime.S = S; | ||
hirestime.MS = MS; | ||
hirestime.NS = NS; | ||
module.exports = hirestime; |
{ | ||
"name": "hirestime", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "thin wrapper around process.hrtime", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -11,5 +11,17 @@ # hirestime | ||
hirestime is a thin wrapper around `process.hrtime()` that returns an function on invocation. | ||
when these function is invoked the elapsed time in millesconds will be returned: | ||
hirestime is a thin wrapper around `process.hrtime()` that does the clumsy handling of the returned array for you. | ||
## hirestime() | ||
returns a function: | ||
### returnedFunction([unit]) | ||
returns the elapsed time since the call of `hirestime` in milliseconds. | ||
an optional unit parameter can be specified that will cause an recalculation. | ||
possible parameters | ||
* `hirestime.S` elapsed time in seconds | ||
* `hirestime.MS` elapsed time in milliseoncds | ||
* `hirestime.NS` elapsed time in nanoseconds | ||
## Examples | ||
````javascript | ||
@@ -26,1 +38,14 @@ var hirestime = require('../'); | ||
```` | ||
````javascript | ||
var hirestime = require('../'); | ||
//startpoint of the time measurement | ||
var getElapsed = hirestime(); | ||
setTimeout(function() { | ||
//returns the elapsed seconds | ||
console.log(getElapsed(hirestime.S)); | ||
}, 1000); | ||
```` | ||
@@ -5,14 +5,55 @@ var expect = require('chai').expect; | ||
function hrtimeMock(msTime) { | ||
var _hrtime = process.hrtime; | ||
var returnValue = [ | ||
parseInt(msTime / 1e3, 10), | ||
(msTime % 1000) * 1e6 | ||
]; | ||
var isFirst = true; | ||
process.hrtime = function() { | ||
if(isFirst) { | ||
isFirst = false; | ||
return []; | ||
} else { | ||
isFirst = true; | ||
process.hrtime = _hrtime; | ||
return returnValue; | ||
} | ||
} | ||
} | ||
describe('hirestime', function() { | ||
it('should return an approximate number of elapsed milliseconds', function(done) { | ||
it('should return an approximate number of elapsed time in milliseconds (no unit given)', function() { | ||
hrtimeMock(100); | ||
var getElapsed = hirestime(); | ||
setTimeout(function() { | ||
expect(getElapsed()).to.be.within(990, 1100); | ||
expect(getElapsed()).to.equal(100); | ||
}); | ||
done(); | ||
}, 1000); | ||
it('should return an approximate number of elapsed time in seconds (seconds unit)', function() { | ||
hrtimeMock(100); | ||
var getElapsed = hirestime(); | ||
expect(getElapsed(hirestime.S)).to.equal(0.1); | ||
}); | ||
it('should return an approximate number of elapsed time in seconds (milliseconds unit)', function() { | ||
hrtimeMock(100); | ||
var getElapsed = hirestime(); | ||
expect(getElapsed(hirestime.MS)).to.equal(100); | ||
}); | ||
it('should return an approximate number of elapsed time in seconds (nanoseconds unit)', function() { | ||
hrtimeMock(100); | ||
var getElapsed = hirestime(); | ||
expect(getElapsed(hirestime.NS)).to.equal(100000); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5416
67
50