Skip to content

Instantly share code, notes, and snippets.

@samgiles
Created March 30, 2015 15:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samgiles/a738a65bbf3b1dfb475b to your computer and use it in GitHub Desktop.
Save samgiles/a738a65bbf3b1dfb475b to your computer and use it in GitHub Desktop.
hrtimer.js
// 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