Created
March 30, 2015 15:54
-
-
Save samgiles/a738a65bbf3b1dfb475b to your computer and use it in GitHub Desktop.
hrtimer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage: | |
// | |
// ```JS | |
// var timer = new Timer(); | |
// | |
// timer.start("javascript"); | |
// | |
// compile(function(e, callback) { | |
// var timeInSecs = timer.end("javascript"); | |
// console.log("It took: ", timeInSecs, "s"); | |
// }); | |
// | |
function Timer() { | |
this.startTime = {}; | |
} | |
Timer.prototype.start = function(name) { | |
this.startTime[name] = process.hrtime(); | |
}; | |
Timer.prototype.end = function(name) { | |
var diff = process.hrtime(this.startTime[name]); | |
return hrtimeInSecs(diff); | |
}; | |
function hrtimeInSecs(hrtime) { | |
return hrtime[0] + (hrtime[1] / 1.0e9); | |
} | |
module.exports = Timer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment