Array.indexOf vs Object's "in operator

JavaScript performance comparison

Revision 18 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 = [], 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';
};
</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
if (arr.indexOf('first')){};
pending…
Array - Find last
if (arr.indexOf('last')){};
pending…
Array - Find nonexistent
if (arr.indexOf('foo')){};
pending…
Object - Test first
if (obj.first){};
pending…
Object - Test last
if (obj.last){};
pending…
Object - Test nonexistent
if (obj.foo){};
pending…
Object - Find first
if ('first' in obj){};
pending…
Object - Find last
if ('last' in obj){};
pending…
Object - Find nonexistent
if ('foo' in obj){};
pending…
Object - Typeof first against undefined
if (typeof obj['first'] !== 'undefined'){};
pending…
Object - Typeof last against undefined
if (typeof obj['last'] !== 'undefined'){};
pending…
Object - Typeof nonexistent against undefined
if (typeof obj['foo'] !== 'undefined'){};
pending…
Object - Test first against undefined
if (obj['first'] !== undefined){};
pending…
Object - Test last against undefined
if (obj['last'] !== undefined){};
pending…
Object - Test nonexistent against undefined
if (obj['foo'] !== undefined){};
pending…
Object - Test first against undefined with attr access
if (obj.first !== undefined){};
pending…
Object - Test last against undefined with attr access
if (obj.last !== undefined){};
pending…
Object - Test nonexistent against undefined with attr access
if (obj.foo !== undefined){};
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