Use a basic time tracker instead of Date manipulation

This commit is contained in:
Cryptkeeper
2015-11-03 01:40:09 -06:00
parent fcff7a0fec
commit 3373ebc9ee
4 changed files with 49 additions and 14 deletions

31
lib/profiler.js Normal file
View File

@ -0,0 +1,31 @@
var logger = require('./logger');
var timestamps = {};
function getCurrentTimeMs() {
return (new Date).getTime();
};
exports.track = function(name) {
if (timestamps[name]) {
throw new Error(name + ' is already being profiled!');
}
timestamps[name] = getCurrentTimeMs();
};
exports.untrack = function(name) {
if (!timestamps[name]) {
throw new Error(name + ' isn\'t being profiled!');
}
var timestamp = getCurrentTimeMs() - timestamps[name];
delete timestamps[name];
logger.log('debug', name + ' took ' + timestamp + 'ms');
return timestamp;
};
exports.getCurrentTimeMs = getCurrentTimeMs();