Loop vs map vs forEach

JavaScript performance comparison

Revision 14 of this test case created by Fredrik Borgström

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var objects = [];
  
  for (var i = 0; i < 10000; i++) {
    range.push({
      name: "Hello",
      value: 5
    });
  }

};
</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
while
var obj,
  item,
  ln = range.length,
  result = new Array(ln);

while (--ln > -1) {
  item = range[ln];
   item.name = 'hi';
  item.value = 6;
  result[ln] = obj;
}
pending…
forEach
var result = new Array(range.length);

range.forEach(function(item) {
  item.name = 'hi';
  item.value = 6;
  result.push(item);
});
pending…
Loop
var i,
  item,
  ln = range.length,
  result = new Array(ln);

for (i = 0; i < ln; i++) {
  item = range[i];
  item.name = 'hi';
  item.value = 6;
}
pending…
map
var result = range.map(function(item) {
  item.name = 'hi';
  item.value = 6;
  return item;
});
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.

0 Comments