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

Revision 38 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) {
  // 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
Object - hasOwnProperty (first element)
if (obj.hasOwnProperty('that')) {
  // found
}
ready

Revisions

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