Array.indexOf vs Object's "in operator

JavaScript performance comparison

Revision 14 of this test case created by zahar

Info

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

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = [
       'that',
       'is',
       'some',
       'nice',
       'some',
       'test',
       'here',
    ]
   
    var obj = {
       'that' : null,
       'is' : null,
       'some' : null,
       'nice' : null,
       'test' : null,
       'here' : 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 element
if (arr.indexOf('that') !== -1) {
  // found
}
pending…
Object - Find first element
if ('that' in obj) {
  // found
}
pending…
Array - Find last element
if (arr.indexOf('here') !== -1) {
  // found
}
pending…
Object - Find last element
if ('here' in obj) {
  // found
}
pending…
Object - Find first element (typeof)
if (typeof obj['that'] !== "undefined") {
  // found
}
pending…
Object - Find last element (typeof)
if (typeof obj['here'] !== "undefined") {
  // found
}
pending…
Object - Find first element
if (obj['that']) {
  // found
}
pending…
Object - Find last element
if (obj['here']) {
  // found
}
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