Return to Snippet

Revision: 58084
at June 24, 2012 19:28 by davidwaterston


Initial Code
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().

  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();         

});

Initial URL

                                

Initial Description
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. 
Use it to get more accurate performance timings when optimizing your code.

Initial Title
Cross-browser, high resolution timer function (replacement for Date().getTime()).

Initial Tags

                                

Initial Language
JavaScript