jQuery each+push vs filter+map

JavaScript performance comparison

Revision 2 of this test case created by benjamingr

Info

Performance difference between using an array and each against filter-map in performing a limited iteration.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    var array = [];
   
    for (var i = 0; i < 100; ++i) {
      array.push(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
array + each
var newArray = [];

$.each(array, function(i, value) {
  if (i === 50) return false;
  newArray.push(value * 7);
})
pending…
filter + map
var newArray = $(array).filter(function(i) {
  return i < 50;
}).map(function(i, value) {
  return value * 7
}).get();
pending…
map + filter
var newArray = $(array).map(function(i, value) {
  return value * 7
}).filter(function(i) {
  return i < 50;
}).get();
pending…
Native loop
var res = [];
for(var i=0;i<array.length;i++){
    if(i>=50) break;
    res.push(array[i]*7);
}
pending…
Native loop until 50
var res = [];
for(var i=0;i<=50;i++){
    res.push(array[i]*7);
}
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