Javascript HTTP request time

Is it possible to get the time of how long it took to send an http request in javascript? This is my code right now:

function httpGet(theUrl)
{
    t = new Date().getTime()
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", theUrl, false );
    xmlHttp.send( null );
    return new Date().getTime() - t;
}

The time in milliseconds returned is a bit inaccurate, is there a way to have a more accurate time displayed?

Possible issues:

  • If you’re testing with a URL that’s cacheable, it might end up not doing any HTTP.
  • Date uses wall time, which might get adjusted and throw off comparisons. performance.now() - Web APIs | MDN Supposedly performance.now works better.

Also, unrelated to this question, I’m required to say “oh no you’re using synchronous xhr.”

3 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.