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

Revision 18 of this benchmark created on


Description

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

Setup

var arr = [], obj = {};
    for (var i=0; i<=10000; i++){
       arr[i] = obj[i] = null;
    }
    
    arr[0] = 'first';
    arr[10000] = 'last';
    
    delete obj[0];
    delete obj[10000];
    obj.first = obj.last = 'null';

Test runner

Ready to run.

Testing in
TestOps/sec
Array - Find first
if (arr.indexOf('first')){};
ready
Array - Find last
if (arr.indexOf('last')){};
ready
Array - Find nonexistent
if (arr.indexOf('foo')){};
ready
Object - Test first
if (obj.first){};
ready
Object - Test last
if (obj.last){};
ready
Object - Test nonexistent
if (obj.foo){};
ready
Object - Find first
if ('first' in obj){};
ready
Object - Find last
if ('last' in obj){};
ready
Object - Find nonexistent
if ('foo' in obj){};
ready
Object - Typeof first against undefined
if (typeof obj['first'] !== 'undefined'){};
ready
Object - Typeof last against undefined
if (typeof obj['last'] !== 'undefined'){};
ready
Object - Typeof nonexistent against undefined
if (typeof obj['foo'] !== 'undefined'){};
ready
Object - Test first against undefined
if (obj['first'] !== undefined){};
ready
Object - Test last against undefined
if (obj['last'] !== undefined){};
ready
Object - Test nonexistent against undefined
if (obj['foo'] !== undefined){};
ready
Object - Test first against undefined with attr access
if (obj.first !== undefined){};
ready
Object - Test last against undefined with attr access
if (obj.last !== undefined){};
ready
Object - Test nonexistent against undefined with attr access
if (obj.foo !== undefined){};
ready

Revisions

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