Created
August 12, 2015 13:31
-
-
Save annapoulakos/a054ca516fdc5c9242ff to your computer and use it in GitHub Desktop.
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
class Timer { | |
constructor () { | |
this.timers = {}; | |
} | |
/** | |
* Starts the timer for the provided identifier | |
* @usage timer.start('myTimer'); | |
* @param {string} id The identifier to use for this particular timer. | |
*/ | |
start (id) { | |
this.timers[id] = this.timers[id] || {}; | |
this.timers[id].start = performance.now(); | |
} | |
/** | |
* Ends the timer for the provided identifier | |
* Calculates the delta between start and end times | |
* @usage timer.end('myTimer'); | |
* @param {string} id The identifier for this particular timer. | |
*/ | |
end (id) { | |
this.timers[id] = this.timers[id] || {}; | |
this.timers[id].end = performance.now(); | |
this.timers[id].delta = this.timers[id].hasOwnProperty('start')? this.timers[id].end - this.timers[id].start: 0; | |
} | |
/** | |
* Gets a specific timer's information | |
* @usage console.log(timer.get('myTimer'); | |
* @param {string} id The identifier of the timer being retrieved | |
* @return {object} The timer object of the provided identifier | |
*/ | |
get (id) { | |
return this.timers[id]; | |
} | |
/** | |
* Returns all timers | |
* @usage console.table(timer.list()); | |
* @return {object} Returns the internal timers object | |
*/ | |
list () { | |
return this.timers; | |
} | |
} |
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
var Timer = function () { | |
this.timers = {}; | |
}; | |
Timer.prototype.start = function (id) { | |
this.timers[id] = this.timers[id] || {}; | |
this.timers[id].start = performance.now(); | |
}; | |
Timer.prototype.end = function (id) { | |
this.timers[id] = this.timers[id] || {}; | |
this.timers[id].end = performance.now(); | |
this.timers[id].delta = this.timers[id].hasOwnProperty('start')? this.timers[id].end - this.timers[id].start: 0; | |
}; | |
Timer.prototype.get = function (id) { | |
return this.timers[id]; | |
}; | |
Timer.prototype.list = function () { | |
return this.timers; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment