find element in obj vs array

JavaScript performance comparison

Revision 7 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var obj = {},
        arr = [],
        count = 0;
    for (var i = 0; i < 1000; i++) {
      arr.push(i);
      obj[i] = 1;
    }
   
   
    function in_array(needle, haystack) {
      for (var i = 0, maxi = haystack.length; i < maxi; ++i) {
        if (haystack[i] == needle) {
          return true;
        }
      }
      return false;
    }
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
find in array (1)
if (in_array(1, arr)) {
  count++;
}
pending…
find in array (5)
if (in_array(5, arr)) {
  count++;
}
pending…
find in array (20)
if (in_array(20, arr)) {
  count++;
}
pending…
find in obj (1)
if (obj[1]) {
  count++;
}
pending…
find in obj (5)
if (obj[1000]) {
  count++;
}
pending…
find in obj (20)
if (obj[20]) {
  count++;
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment