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

Revision 25 of this benchmark created on


Description

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

Setup

var arr = [
       'that',
       'is',
       'some',
       'nice',
       'some',
       'test',
       'here',
    ]
    
    var obj = {
       'that' : null,
       'is' : null,
       'some' : null,
       'nice' : null,
       'test' : null,
       'here' : null
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Array - Find first element
if (arr.indexOf('that') !== -1) {
  return;
}
ready
Object - Find first element
/*if ('that' in obj) {
  // found
}*/

        for (var i = 0; i < arr.length; i++) {
            if (arr[i] === "that") {
                return true;
            }
        }
ready
Array - Find last element
if (arr.indexOf('here') !== -1) {
  // found
}
ready
Object - Find last element
if ('here' in obj) {
  // found
}
ready
Object - Find first element (typeof)
if (typeof obj['that'] !== "undefined") {
  // found
}
ready
Object - Find last element (typeof)
if (typeof obj['here'] !== "undefined") {
  // found
}
ready
Object - Find first element
if (obj['that'] !== undefined) {
  // found
}
ready
Object - Find last element
if (obj['here'] !== undefined) {
  // found
}
ready

Revisions

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