Last active
December 19, 2016 12:22
-
-
Save stephenfeather/9efad19d857d4151119183f3f3b761d8 to your computer and use it in GitHub Desktop.
Quick and dirty timings libary, most definitely should not be used for engineering, medical, safety related usages.
This file contains hidden or 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
/* | |
Requires Underscore | |
Basic Usage: | |
var Timing = require('Timing'); | |
Timing.start('myLabel'); | |
Timing.stop('myLabel'); | |
Timing.getTime('myLabel'); | |
Dump raw time marks to console: | |
Timing.dump(); | |
Dump calculated time w/ label to console: | |
Timing.calculatedDump(); | |
*/ | |
var timingTable = {}; | |
function Timings() { | |
} | |
Timings.prototype.clear = function(name) { | |
delete timingTable[name]; | |
}; | |
Timings.prototype.start = function(name) { | |
// console.log(name); | |
timingTable[name] = {}; | |
timingTable[name].start = Date.now(); | |
}; | |
Timings.prototype.stop = function(name) { | |
timingTable[name].stop = Date.now(); | |
if (!timingTable[name] && !timingTable[name].start) { | |
return null; | |
} else { | |
return (timingTable[name].stop - timingTable[name].start); | |
} | |
}; | |
Timings.prototype.dump = function() { | |
console.log(timingTable); | |
}; | |
Timings.prototype.calculatedDumps = function() { | |
_.each(timingTable, function(value, key) { | |
var temp = key + ': ' + (timingTable[key].stop - timingTable[key].start); | |
console.log(temp); | |
}); | |
}; | |
Timings.prototype.getTime = function(name) { | |
if (!timingTable[name] && !timingTable[name].start) { | |
return null; | |
} | |
if (timingTable[name].stop) { | |
return (timingTable[name].stop - timingTable[name].start); | |
} else { | |
return (Date.now() - timingTable[name].start); | |
} | |
}; | |
var timing = new Timings(); | |
module.exports = timing; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment