From 697749b204420eb69261d8d0ed1c62da0254d3b2 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Fri, 18 Mar 2016 17:21:43 +0100 Subject: [PATCH] Add timer helper --- lib/cartodb/stats/timer.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 lib/cartodb/stats/timer.js diff --git a/lib/cartodb/stats/timer.js b/lib/cartodb/stats/timer.js new file mode 100644 index 00000000..0ef725ab --- /dev/null +++ b/lib/cartodb/stats/timer.js @@ -0,0 +1,32 @@ +function Timer() { + this.times = {}; +} + +module.exports = Timer; + +Timer.prototype.start = function(label) { + this.timeIt(label, 'start'); +}; + +Timer.prototype.end = function(label) { + this.timeIt(label, 'end'); +}; + +Timer.prototype.timeIt = function(label, what) { + this.times[label] = this.times[label] || {}; + this.times[label][what] = Date.now(); +}; + +Timer.prototype.getTimes = function() { + var self = this; + var times = {}; + + Object.keys(this.times).forEach(function(label) { + var stat = self.times[label]; + if (stat.start && stat.end) { + times[label] = Math.max(0, stat.end - stat.start); + } + }); + + return times; +};