Finding intersection of array

JavaScript performance comparison

Test case created by ugo

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var evenNumbers = [];
  var divBySeven = [];
  
  for (var i = 2; i < 1000; i += 2) {
      evenNumbers.push({
          name: i.toString(),
          id: i
      });
  }
  
  for (var j = 7; j < 1000; j += 7) {
      divBySeven.push({
          name: j.toString(),
          id: j
      });
  }

};

Benchmark.prototype.teardown = function() {
  evenNumbers = [];
  divBySeven = [];

};
</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
Nested for-loops
var result1 = [];

for (var k = 0; k < evenNumbers.length; k++) {
    for(var l = 0; l < divBySeven.length; l++) {
        if (evenNumbers[k].id === divBySeven[l].id) {
            // Push factor of 2 and 7
            result1.push(evenNumbers[k]);
            break;
        }
    }
}
pending…
filter() + findIndex()
var result2 = [];

var result2 = evenNumbers.filter(function (evenNumber) {
    return divBySeven.findIndex(function (factorOfSeven) {
        // Match factor of 2 and 7
        return factorOfSeven.id === evenNumber.id;
    }) >= 0;
});
pending…

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

0 Comments