Array intersectioning

JavaScript performance comparison

Test case created by Krinkle

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var foo = [1, 10, 7, 4, 8, 9, 2, 300, 400, 500, 600, 700, 800],
        bar = [6, 1, 0, 100, 400, 900, 600];
   
    function test_indexOf(a, b) {
      var i, len, c = [];
      for (i = 0, len = a.length; i < len; i++) {
        if (b.indexOf(a[i]) !== -1) {
          c.push(a[i]);
        }
      }
      return c;
    }
   
    function test_forFor(a, b) {
      var i, j, ilen, jlen, c = [];
      for (i = 0, ilen = a.length; i < ilen; i++) {
        for (j = 0, jlen = b.length; j < jlen; j++) {
          if (a[i] === b[j]) {
            c.push(a[i]);
            break;
          }
        }
      }
      return c;
    }
};
</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
indexOf
test_indexOf(foo, bar);
pending…
forFor
test_forFor(foo, bar);
pending…

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

Compare results of other browsers

0 comments

Add a comment