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

Revision 10 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',
       '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) {
  // found
}
ready
Object - Find first element
if ('that' in obj) {
  // found
}
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
hasOwnProperty
if (obj.hasOwnProperty('here')){
  // found
}
ready
array access
if (arr[3] !== "undefined"){
  // found
}
ready
array access2
if ((typeof arr[3]) !== "undefined"){
  // found
}
ready
Object 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.