filter vs foreach

JavaScript performance comparison

Test case created by Josh

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var data = [];
    for (var i = 0; i < 1000; i++) {
      data[i] = {value: i};  
    }
    var countA = 0;
    var countB = 0;
   
};

Benchmark.prototype.teardown = function() {
    console.log(countA, countB);
};
</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
forEach
data.forEach(function(v) {
  if (v.value % 13) {
    countA++;
  }
});
pending…
filter
countB = data.filter(function(v) {
  return (v.value % 13);
}).length;
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