Is XDomainRequest slower than XMLHttpRequest?

139 Views Asked by At

I am running a simple test to measure download speed using JS only. The script uses an HTTP request to download a file from a different server (which allows cross-domain requests).

Everything works beautifully in modern browsers using XMLHttpRequest. I have to support IE 8 and 9, so in those browsers I use XDomainRequest. Again, everything works -- but the results are very different than in modern browsers.

In modern browsers on my network I consistently get a download speed of 300-400 Mbps. In IE8/9, download speed is consistently 20-40 Mbps.

I can't spot anything wrong with my code, but before I tear it apart statement by statement I have to ask: is XDR inherently slower than XHR? I would love to be able to say, "Sorry, boss, these are the limitations we face when forced to support older, flawed browsers," but I need to be sure.

For the sake of completeness -- although there's nothing remarkable about it -- I include a simplified version of my script:

var isXDR = window.XDomainRequest && (window.XMLHttpRequest && new XMLHttpRequest().responseType === undefined),
    hasPerformanceAPI = window.performance && 'now' in window.performance,
    req,
    url = 'http://xdomain.server.com/file.bin',
    startTime,
    duration,
    responseHandler = function () {
        duration = (hasPerformanceAPI ? window.performance.now() : +new Date()) - startTime;
        /* do more stuff */
    };

startTime = hasPerformanceAPI ? window.performance.now() : +new Date();

if (isXDR) {
    req = new XDomainRequest();
    req.onload = responseHandler;
    req.open('GET', url);
} else {
    req = new XMLHttpRequest();
    req.open('GET', url, true);
    req.onreadystatechange = function () {
        if (req.readyState === 4) {
            responseHandler();
        }
    };
}
req.ontimeout = function () {/* do stuff */};
req.timeout = 10000;
req.send(null);

So, is the discrepancy between IE8/9 and modern browsers due to XDR being slower than XHR, or do I need to sift through my code more closely?

0

There are 0 best solutions below