Skip to content

Instantly share code, notes, and snippets.

@stephenfeather
Last active December 19, 2016 12:22
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 stephenfeather/9efad19d857d4151119183f3f3b761d8 to your computer and use it in GitHub Desktop.
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.
/*
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