map/filter vs foreach

JavaScript performance comparison

Test case created by William Lachance and last updated

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var a = Array(100000);
    var i;
    for (i=0; i<100000; i++) { a[i] = i; }
};
</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
map/filter
var b = a.filter(function(c) { return c<0.5; }).map(function(c) { return c+1 });
pending…
for loop
var b=[];
for (i=0; i<a.length; i++) { if (a[i] < 0.5) { b.push(a[i]+1); } }
pending…
foreach
var b=[];
a.forEach(function(c) { if (c<0.5) { b.push(c+1) } })
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