2016-04-18 17:20:30 -04:00
|
|
|
/**
|
2016-09-12 13:09:01 -04:00
|
|
|
* @fileoverview
|
|
|
|
* A utility for accurately measuring time.
|
|
|
|
* To use:
|
|
|
|
* ---
|
|
|
|
* var timer = new Timer();
|
|
|
|
* timer.start();
|
|
|
|
* ... pass some time ...
|
|
|
|
* var timeDifference = timer.timeElapsed();
|
|
|
|
* ---
|
|
|
|
* Or, you can use the `time` and `relativeTime`
|
|
|
|
* to do some measurement yourself.
|
2016-04-18 17:20:30 -04:00
|
|
|
*/
|
|
|
|
|
2016-09-12 13:09:01 -04:00
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
*/
|
2017-04-17 15:10:04 -04:00
|
|
|
const Timer = function () {};
|
2016-09-12 13:09:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to store the start time of a timer action.
|
|
|
|
* Updated when calling `timer.start`.
|
|
|
|
*/
|
|
|
|
Timer.prototype.startTime = 0;
|
|
|
|
|
2017-02-08 03:11:10 -05:00
|
|
|
/**
|
|
|
|
* Disable use of self.performance for now as it results in lower performance
|
|
|
|
* However, instancing it like below (caching the self.performance to a local variable) negates most of the issues.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2017-04-17 15:10:04 -04:00
|
|
|
const USE_PERFORMANCE = false;
|
2017-02-08 03:11:10 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Legacy object to allow for us to call now to get the old style date time (for backwards compatibility)
|
|
|
|
* @deprecated This is only called via the nowObj.now() if no other means is possible...
|
|
|
|
*/
|
2017-04-17 15:10:04 -04:00
|
|
|
const legacyDateCode = {
|
2017-02-08 03:11:10 -05:00
|
|
|
now: function () {
|
|
|
|
return new Date().getTime();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use this object to route all time functions through single access points.
|
|
|
|
*/
|
2017-04-17 15:10:04 -04:00
|
|
|
const nowObj = (USE_PERFORMANCE && typeof self !== 'undefined' && self.performance && 'now' in self.performance) ?
|
2017-02-08 03:11:10 -05:00
|
|
|
self.performance : Date.now ? Date : legacyDateCode;
|
|
|
|
|
2016-09-12 13:09:01 -04:00
|
|
|
/**
|
|
|
|
* Return the currently known absolute time, in ms precision.
|
|
|
|
* @returns {number} ms elapsed since 1 January 1970 00:00:00 UTC.
|
|
|
|
*/
|
2017-02-08 03:11:10 -05:00
|
|
|
Timer.prototype.time = function () {
|
|
|
|
return nowObj.now();
|
|
|
|
};
|
2016-04-18 17:20:30 -04:00
|
|
|
|
2016-09-12 13:09:01 -04:00
|
|
|
/**
|
|
|
|
* Returns a time accurate relative to other times produced by this function.
|
|
|
|
* If possible, will use sub-millisecond precision.
|
|
|
|
* If not, will use millisecond precision.
|
|
|
|
* Not guaranteed to produce the same absolute values per-system.
|
|
|
|
* @returns {number} ms-scale accurate time relative to other relative times.
|
|
|
|
*/
|
2017-02-08 03:11:10 -05:00
|
|
|
Timer.prototype.relativeTime = function () {
|
|
|
|
return nowObj.now();
|
|
|
|
};
|
2016-09-12 13:09:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Start a timer for measuring elapsed time,
|
|
|
|
* at the most accurate precision possible.
|
|
|
|
*/
|
2016-04-18 17:20:30 -04:00
|
|
|
Timer.prototype.start = function () {
|
2017-02-08 03:11:10 -05:00
|
|
|
this.startTime = nowObj.now();
|
2016-04-18 17:20:30 -04:00
|
|
|
};
|
|
|
|
|
2016-04-26 15:00:45 -04:00
|
|
|
Timer.prototype.timeElapsed = function () {
|
2017-02-08 03:11:10 -05:00
|
|
|
return nowObj.now() - this.startTime;
|
2016-04-18 17:20:30 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Timer;
|