0

/*

Is it possible to turn the first block into the second block? I've done something similar with requestAnimationFrame, but because of the dot it does not seem to work here.

*/

////////////////////////////////////////////////////////////////////////////////////////

window.performance = window.performance || {};

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

var PeRfOrMaNcE = window.performance;

console.log(PeRfOrMaNcE.now());

////////////////////////////////////////////////////////////////////////////////////////

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

console.log(PeRfOrMaNcE());
Esailija
  • 130,716
  • 22
  • 250
  • 308
wubbewubbewubbe
  • 701
  • 1
  • 6
  • 19
  • 2
    I pity the guy that has to followup this code and remember how you've capitalised `PeRfOrMaNcE` :( – Alnitak Oct 23 '13 at 18:16
  • 1
    @Alnitak it looks like he didn't want to confuse it with performance. Calling it something else entirely would have been easier I think. – LuFaMa Oct 23 '13 at 18:21

2 Answers2

1

At least in Chrome, the now() function requires that this === window.performance.

You therefore have to use .call or .bind to invoke it correctly.

This appears to work, although it'll still require that window.performance exists, even if it's just initialised to an empty object per your first code attempt:

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

or alternately, avoiding the .bind call when window.performance doesn't exist:

var PeRfOrMaNcE = (function()
{
     var wp = window.performance;
     var now = wp && (wp.now || wp.webkitNow || wp.msNow || wp.mozNow || wp.oNow);

     return now && now.bind(wp) || function() {
         return new Date().getTime();
     }
})();
Alnitak
  • 313,276
  • 69
  • 379
  • 466
1

Fixed.

Concise, but slightly more obfuscated.

var PeRfOrMaNcE = function()
{
    var wp = window.performance;
    var v = ['now', 'webkitNow','msNow','mozNow', 'oNow'];
    for (var i in v) {
        if (wp[v[i]]) return wp[v[i]]()
    }
    return (new Date()).getTime();
}
colbydauph
  • 363
  • 1
  • 6
  • 1
    Did you run that code? http://jsfiddle.net/P3MCZ/ You are `return`ing inside the `forEach` callback so the `new Date()` version is used regardless of browser used. – Sergiu Paraschiv Oct 23 '13 at 18:42
  • the code is also inefficient because it evaluates the test for which version of `.now` to call _every time the function is invoked_.... – Alnitak Oct 23 '13 at 19:05
  • Your code was the first to work. I picked Alnitak's answer because of the self invoking function. I upvoted your answer, but I can not pick 2 answers. – wubbewubbewubbe Oct 23 '13 at 19:42