in object vs inArray

Benchmark created by Alexis Deveria on


Description

Is it faster to check if a value is in an object or an array?

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
 var obj = {
  a: true,
  b: true,
  c: true,
  d: true,
  e: true,
  f: true,
  g: true
 };
 
 var inArr = $.inArray;
 var arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Object
var hasProp = 'd' in obj;
ready
Array using: jQuery's $.inArray
var hasProp = !! ~inArr('d', arr);
ready
Array using indexOf
var hasProp = !! arr.indexOf('d');
ready

Revisions

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

  • Revision 1: published by Alexis Deveria on