Array.indexOf vs Object's "in operator

JavaScript performance comparison

Revision 15 of this test case created

Info

Testing if access through Array.indexOf or Object's in Operator is faster.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = [];
    var obj = {};
    for (var i=0; i<=10000; i++){
       arr.push({id: i, name: "hello"});
       obj[i] = {id: i, name: "hello"};
    }
    Array.prototype.indexOfObj = function (property, value, returnIndex) {
        for (var i = 0, len = this.length; i < len; i++)
            if (this[i][property] === value) return returnIndex ? i : this[i];
        return returnIndex ? -1 : false;
    };
   
};
</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
Array - Find first element
if (arr.indexOfObj("id", 10000)){};
pending…
Object - Find first element
if (obj[10000]){};
pending…
Array - Find last element
var obj = {"id": 123}
 
pending…
Object - Find last element
obj[32421] = 123;
pending…
Object - Find first element (typeof)
delete obj[1000];
pending…
Object - Find last element (typeof)
 
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