find element in obj vs array

JavaScript performance comparison

Test case created by

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 CCBot 2.0.0 / Other 0.0.0
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 obj (5)
if (obj[5]) {
  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 (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.

0 Comments