jQuery.ajax vs custom ajax

Benchmark created by ThinkingStiff on


Preparation HTML

<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

Revisions

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