Skip to content

Instantly share code, notes, and snippets.

@davidwaterston
Created June 24, 2012 09:00
Show Gist options
  • Save davidwaterston/2982531 to your computer and use it in GitHub Desktop.
Save davidwaterston/2982531 to your computer and use it in GitHub Desktop.
A cross-browser Javascript shim function to return the number of milliseconds elapsed since either the browser navigationStart event (using performance.now or browser equivalent) or the UNIX epoch, depending on availability.
var now = (function() {
// 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(); };
})();
return performance.now();
});
@JordanDelcros
Copy link

'performance.now()' and 'Date().getTime()' do not return the same kind of time.
The first one return the current elapsed time since JavaScript engine start (approximately), the second one return the current elapsed time since january 1st 1970...
I made a gist to return a more accurate shim
https://gist.github.com/JordanDelcros/30845e2dbc0b21ac0869

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment