Search code examples
xmlhttprequestchromiumhead

Chromium Browser first HEAD request takes significantly higher time


I am using this Javascript code to send head requests and log time taken for each request:

var url = "https://www.someexampleurl.com"
var xmlhttp = new XMLHttpRequest();
xmlhttp.responseType = "json";var t0 = performance.now();
xmlhttp.open("HEAD", url, true);
xmlhttp.onreadystatechange= function() {
    if(xmlhttp.readyState==4){
        var t1 = performance.now() - t0;
        resp = xmlhttp.getAllResponseHeaders();
        console.log(t1);
};};
xmlhttp.send();

I noticed that the first request took ~614ms, all the subsequent ones took ~170ms. This was strange, so I launched chromium with --disable-web-security flag to allow CORS requests, and tried sending HEAD requests to google and yahoo. The results were same. First request would take significantly higher than the subsequent requests.

I tried using PyCurl, and time to complete request were more consistent in that case, which makes me wonder if this is related to some chromium setting? for instance some option to disable caching etc that might fix this?


Solution

  • I have figured it out, after some searching. The extra time taken during the first HEAD request is for opening TCP connections. Since Chrome keeps the connection alive for some time (approx. 1 minute), the subsequent requests take less time. In PyCurl, connection is closed when the request is complete, hence each request suffers from the the additional connection time.