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

Revision 39 of this benchmark created by Robert Koritnik 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 Dot notation - Find last element
if (typeof obj.here !== "undefined") {
  // found
}
ready
Object Dot notation - Find las element
if (obj.here !== undefined) {
  // found
}
ready
Object - Find last directly
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.