Array.indexOf vs Object's "in operator (v15)

Revision 15 of this benchmark created on


Description

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

Setup

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;
    };

Test runner

Ready to run.

Testing in
TestOps/sec
Array - Find first element
if (arr.indexOfObj("id", 10000)){};
ready
Object - Find first element
if (obj[10000]){};
ready
Array - Find last element
var obj = {"id": 123}
 
ready
Object - Find last element
obj[32421] = 123;
ready
Object - Find first element (typeof)
delete obj[1000];
ready
Object - Find last element (typeof)
 
ready

Revisions

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