Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(Common): update the Common.now #55

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/core/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,27 @@ var Common = {};
* @return {number} the current timestamp (high-res if avaliable)
*/
Common.now = function() {
// http://stackoverflow.com/questions/221294/how-do-you-get-a-timestamp-in-javascript
// https://gist.github.com/davidwaterston/2982531

var perf = window.performance;

if (perf) {
perf.now = perf.now || perf.webkitNow || perf.msNow || perf.oNow || perf.mozNow;
return +(perf.now());
}

return +(new Date());
// Returns the number of milliseconds elapsed since either the browser navigationStart event or
// the UNIX epoch, depending on availability.
// Where the browser supports 'performance' we use that as it is more accurate (microsoeconds
// will be returned in the fractional part) and more reliable as it does not rely on the system time.
// Where 'performance' is not available, we will fall back to Date().getTime().
// jsFiddle: http://jsfiddle.net/davidwaterston/xCXvJ

var performance = window.performance || {};

performance.now = (function() {
return performance.now ||
performance.webkitNow ||
performance.msNow ||
performance.oNow ||
performance.mozNow ||
function() { return new Date().getTime(); };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should rewrite this line to simply be a reference to Date.now. It outperforms creating a new Date instance and it makes the code cleaner.

Date.now is supported by all ECMAScript 5 browsers (IE 9+).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is migrated to Date.now then it should be compatibility checked as well and Date.getTime() should still be a fallback. Not all clients are capable of supporting IE9+; we use Matter.js on IE6, for instance.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case yes, you could add it as a check before defaulting to new Date().getTime()

})();

return performance.now();
};


Expand Down