inarraytest

JavaScript performance comparison

Revision 3 of this test case created by T.J. Crowder

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    var arr=[0,1,2,3,4,5,6,7,8,9];
   
    function arrayLoop(elem, array, i) {
        var len = array.length;
        i = i ? i < 0 ? Math.max( 0, len + i ) : i : 0;
        for ( ; i < len; i++ ) {
            // Skip accessing in sparse arrays
            if ( i in array && array[ i ] === elem ) {
                return i;
            }
        }
        return -1;
    }
   
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
jQuery.inArray last
var rtn=jQuery.inArray(9,arr);
pending…
arrayLoop last
var rtn = arrayLoop(9,arr);
pending…
jQuery.inArray middle
var rtn=jQuery.inArray(4,arr);
pending…
arrayLoop middle
var rtn=arrayLoop(4,arr);
pending…
jQuery.inArray first
var rtn=jQuery.inArray(0,arr);
pending…
arrayLoop first
var rtn=arrayLoop(0,arr);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment