4

I am using HTML5 Navigation Timing API to measure the user perceived page load time on my site using the following code:

//  Process load time using "Navigation Timing"  
function recordLoadTime ()
{
    if (typeof(window.performance) !== "undefined" 
        && typeof(window.performance.timing) !== "undefined")
    {
        if(window.performance.timing.loadEventEnd > 0)
        {
            var time = window.performance.timing.loadEventEnd  
                   - window.performance.timing.navigationStart;
        } else {
            setTimeout(recordLoadTime, 1000);
        }
    }
}

The time variable is also added to a cookie and recorded on the server in subsequent requests by the user.

I am facing an issue where the recorded time is very close to the current epoch time:

  • i.e. navigationStart is set to 0
  • but loadEventEnd has a non-zero value (i.e. the current epoch time)

I have seen this behavior on Chrome/11.0, Chrome/12.0, MSIE 7.0, MSIE 8.0 and MSIE 9.0

I have temporarily solved this by modifying the above code to record load time only when navigationStart is greater than 0. But I wish to record the load times for all pages served.

surfmuggle
  • 5,527
  • 7
  • 48
  • 77
Nikhil
  • 473
  • 4
  • 11
  • Per the [NavigationTiming spec](http://www.w3.org/TR/navigation-timing/#dom-performancetiming-navigationstart), `navigationStart` should **always** be **non-zero**. Anything else is a bug and should be reported to the vendor. This is likely fixed in recent versions of Chrome as it hasn't been widely reported. Note IE8 did not have NavigationTiming support. – NicJ Jan 14 '14 at 17:45
  • I know this is old, but I thought you'd be happy to know that even Google accounts for the case in which `navigationStart` is 0 in the analytics tracking code. Probably says something about vendor support... – freethejazz Feb 24 '14 at 00:55

1 Answers1

1
window.onload = function()
{
  setTimeout(function(){
  var t = performance.timing;

}, 0);
Jim
  • 189
  • 1
  • 14
  • This would only measure the time that the page took to download subresources, render and fire the load event (discarding DNS, TCP and request/response of the HTML). – NicJ Jan 14 '14 at 17:43
  • Sure, but the question was why is navigationStart 0, the reason being he is asking for the data too soon, needs to wait for the next tick. I'll remove the logging part if that confused the question asker into not marking my answer as correct ;) – Jim Jan 15 '14 at 07:35
  • `navigationStart` should never be 0, since it should represent the number of seconds since Unix epoch that the navigation started. Even if you query it in a script in the head, `navigationStart` should be non-zero. – NicJ Jan 16 '14 at 14:09