XHR vs Image Object init

Benchmark created by iLaurens on


Description

What will be more costly, getting a new XHR object versus a new Image object? The result could lead to the decision whether to log data via XHR GET of a normal GET. Mind that an Image object could block the (on)load event, while XHR won't. So when the data will be send should also be considered within the decision.

Setup

var getXHR = function() {
        if (window.XMLHttpRequest) {
            getXHR = function() {
                return new XMLHttpRequest;
            };
            return new XMLHttpRequest;
        }
    
        var http,
            msxml = [          
                'Microsoft.XMLHTTP',
                'MSXML2.XMLHTTP',
                'MSXML2.XMLHTTP.3.0'
            ],
            iterations = msxml.length;
    
        for (iterations; iterations--;) {
            try {
                http = new ActiveXObject(msxml[iterations]);
                getXHR = function() {
                    return new ActiveXObject(msxml[iterations]);
                };
                break;
            }
            catch(e) {}
        }
        
        return http;
    };

Test runner

Ready to run.

Testing in
TestOps/sec
Init XHR Object
getXHR();
ready
Init Image Object
new Image();
ready
Init multiple XHR Objects
getXHR();
getXHR();
getXHR();
getXHR();
getXHR();
ready
Init multiple Image Objects
new Image();
new Image();
new Image();
new Image();
new Image();
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by iLaurens on