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

Revision 5 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
Array - Find first element w/obj
if (typeof obj['that'] !== "undefined") {
  if (arr.indexOf('that') !== -1) {
    // found
  }
}
ready
Array - Find none element
if (arr.indexOf('abcd') !== -1) {
  // found
}
ready
Array - Find none element w/obj
if (typeof obj['abcd'] !== "undefined") {
  if (arr.indexOf('abcd') !== -1) {
    // found
  }
}
ready

Revisions

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