jQuery.ajax vs custom ajax (v2)

Revision 2 of this benchmark created on


Preparation HTML

<script src="//raw.github.com/mlbli/Craft/master/craft.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Setup

function ajax( uri, settings ) {
    
                var ajax = new window.XMLHttpRequest(),
                    data = settings.type == 'GET' ? '' : settings.data,
                    async = settings.async ? settings.async : true;
                    uri = settings.type == 'GET' ? uri + ( settings.data ? '?' + settings.data : '' ) : uri;
    
                ajax.onreadystatechange = function () {
    
                    if ( ajax.readyState == 4 ) { //response ready
    
                        if ( ajax.status == 200 ) { //success
    
                            if ( settings.success ) settings.success( ajax.responseText, ajax.statusText );
                            if ( settings.complete ) settings.complete( ajax, ajax.statusText );
    
                        } else {
    
                            if ( settings.error ) settings.error( ajax, ajax.status, ajax.statusText );
    
                        };
    
                    };
    
                };
    
                ajax.open( settings.type, uri, async );
    
                if ( settings.headers ) {
    
                    for ( var header in settings.headers ) {
    
                        ajax.setRequestHeader( header, settings.headers[header] );
    
                    };
    
                };
    
                ajax.send( data );
    
            };

Test runner

Ready to run.

Testing in
TestOps/sec
$.ajax GET
$.ajax( 'http://jsperf.com/browse/thinkingstiff', {"type": "GET"});
ready
custom ajax GET
ajax( 'http://jsperf.com/browse/thinkingstiff', {"type": "GET"});
ready
$.ajax POST
$.ajax( 'http://jsperf.com/browse/thinkingstiff', {

    "type": "POST",
    "data": "a=1233&b=asdfasd",
    "headers": { "x-session": "1234" },
    "success": function ( data, status ) {

        // success 

    },
    "error": function ( response, status, error ) {

        // error

    }

});
ready
ajax POST
ajax( 'http://jsperf.com/browse/thinkingstiff', {

    "type": "POST",
    "data": "a=1233&b=asdfasd",
    "headers": { "x-session": "1234" },
    "success": function ( data, status ) {

        // success 

    },
    "error": function ( response, status, error ) {

        // error

    }

});
ready
craft GET
new Craft.AJAX( 'http://jsperf.com/browse/thinkingstiff', {method: "GET", success : function(res){}}).update();
ready
craft POST
new Craft.AJAX( 'http://jsperf.com/browse/thinkingstiff', {method: "POST", success : function(res){}, query: "a=1233&b=asdfasd"}).update();
ready

Revisions

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