mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-25 07:22:33 -05:00
Optimisation - Only check browser compatability once
Disable use of more accurate timer temporarilly due to performance. However, with the rearrange and introduction of the 'nowObj' it turns out most of the delay is in resolving 'self.performance'... so keeping this cached in nowObj actually mitigates most of the delay.
This commit is contained in:
parent
57c222e94f
commit
7d69ecc005
1 changed files with 31 additions and 19 deletions
|
@ -23,15 +23,35 @@ var Timer = function () {};
|
||||||
*/
|
*/
|
||||||
Timer.prototype.startTime = 0;
|
Timer.prototype.startTime = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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}
|
||||||
|
*/
|
||||||
|
var USE_PERFORMANCE = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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...
|
||||||
|
*/
|
||||||
|
var legacyDateCode = {
|
||||||
|
now: function () {
|
||||||
|
return new Date().getTime();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use this object to route all time functions through single access points.
|
||||||
|
*/
|
||||||
|
var nowObj = (USE_PERFORMANCE && typeof self !== 'undefined' && self.performance && 'now' in self.performance) ?
|
||||||
|
self.performance : Date.now ? Date : legacyDateCode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the currently known absolute time, in ms precision.
|
* Return the currently known absolute time, in ms precision.
|
||||||
* @returns {number} ms elapsed since 1 January 1970 00:00:00 UTC.
|
* @returns {number} ms elapsed since 1 January 1970 00:00:00 UTC.
|
||||||
*/
|
*/
|
||||||
Timer.prototype.time = Date.now ?
|
Timer.prototype.time = function () {
|
||||||
function () {
|
return nowObj.now();
|
||||||
return Date.now();
|
|
||||||
} : function () {
|
|
||||||
return new Date().getTime();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -41,12 +61,8 @@ Timer.prototype.time = Date.now ?
|
||||||
* Not guaranteed to produce the same absolute values per-system.
|
* Not guaranteed to produce the same absolute values per-system.
|
||||||
* @returns {number} ms-scale accurate time relative to other relative times.
|
* @returns {number} ms-scale accurate time relative to other relative times.
|
||||||
*/
|
*/
|
||||||
Timer.prototype.relativeTime =
|
Timer.prototype.relativeTime = function () {
|
||||||
(typeof self !== 'undefined' && self.performance && 'now' in self.performance) ?
|
return nowObj.now();
|
||||||
function () {
|
|
||||||
return self.performance.now();
|
|
||||||
} : function () {
|
|
||||||
return this.time();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -54,15 +70,11 @@ Timer.prototype.relativeTime =
|
||||||
* at the most accurate precision possible.
|
* at the most accurate precision possible.
|
||||||
*/
|
*/
|
||||||
Timer.prototype.start = function () {
|
Timer.prototype.start = function () {
|
||||||
this.startTime = this.relativeTime();
|
this.startTime = nowObj.now();
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Check time elapsed since `timer.start` was called.
|
|
||||||
* @returns {number} Time elapsed, in ms (possibly sub-ms precision).
|
|
||||||
*/
|
|
||||||
Timer.prototype.timeElapsed = function () {
|
Timer.prototype.timeElapsed = function () {
|
||||||
return this.relativeTime() - this.startTime;
|
return nowObj.now() - this.startTime;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = Timer;
|
module.exports = Timer;
|
||||||
|
|
Loading…
Reference in a new issue