Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Performance.now() polyfill
(function(w){
var perfNow;
var perfNowNames = ['now', 'webkitNow', 'msNow', 'mozNow'];
if(!!w['performance']) for(var i = 0; i < perfNowNames.length; ++i)
{
var n = perfNowNames[i];
if(!!w['performance'][n])
{
perfNow = function(){return w['performance'][n]()};
break;
}
}
if(!perfNow)
{
perfNow = Date.now;
}
w.perfNow = perfNow;
})(window);
// Usage
console.log(window.perfNow());
@meowgorithm

This comment has been minimized.

Copy link

meowgorithm commented Dec 5, 2012

Note that Date.now() doesn't exist in IE8, so you may also need to add something along the lines of:

if(Date.now) {
    perfNow = Date.now();
} else {
    perfNow = +(new Date());
}
@dsamarin

This comment has been minimized.

Copy link

dsamarin commented Jun 1, 2013

+(new Date()) // this is a number
function now() { return +(new Date()); } // this is a function that returns a number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.