Custom XHR vs jQuery AJAX (v6)

Revision 6 of this benchmark created by Ted on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>

Setup

function xhr(options, callback) {
        var xhr = new XMLHttpRequest(),
            method = options.method || 'get';
    
        xhr.onreadystatechange = function() {
            if (this.readyState === 4) {
                callback(this.response || this.responseText || this.responseXML);
            }
        }
    
        xhr.onerror = function() {
            callback(null);
        }
    
        xhr.open(method, options.uri);
    
        if (options.headers) {
            Object.keys(options.headers).forEach(function(key) {
                xhr.setRequestHeader(key, options.headers[key]);
            });
        }
    
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    
        //xhr.send((options.data) ? urlstringify(options.data) : null);
    
        return xhr;
    };

Test runner

Ready to run.

Testing in
TestOps/sec
Custom XHR
// async test
xhr({ method: 'get',
      uri: 'http://jsperf.com',
     }, function() {}
);
ready
jQuery AJAX
// async test
var request = $.ajax({
  type: "GET",
  url: "http://jsperf.com",
  success: function() {},
  data: null
});
ready

Revisions

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

  • Revision 1: published by Tommy Bergeron on
  • Revision 2: published by daiyam on
  • Revision 3: published by Przemyslaw Ciezkowski on
  • Revision 5: published on
  • Revision 6: published by Ted on