indexOf

JavaScript performance comparison

Test case created by Tyler Young

Preparation code

<script type="text/javascript">
//<![CDATA[
function Array_indexOf(t) {
        for(var i = 0, l = this.length; i < l; ++i)
                if (this[i] === t) return i;
        return -1;
}
Object.defineProperties( Array.prototype,
        { indexOf2 : { value : Array_indexOf } } );
//]]>
</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
native indexOf
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14].indexOf(8);
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14].indexOf(14);
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14].indexOf(-1);
pending…
custom indexOf
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14].indexOf2(8);
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14].indexOf2(14);
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14].indexOf2(-1);
pending…

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

Compare results of other browsers

0 comments

Add a comment