Loop vs map vs forEach

JavaScript performance comparison

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

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var range = [];
  
  for (var i = 0; i < 5000; 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
map es6
range.map(item => {
  item.name = 'hi';
  item.value = 6;
});
pending…
forEach
range.forEach(function(item) {
  item.name = 'hi';
  item.value = 6;
});
pending…
Loop
var i,
  item,
  ln = range.length;

for (i = 0; i < ln; i++) {
  item = range[i];
  item.name = 'hi';
  item.value = 6;
}
pending…
forEach es6
range.forEach( item => {
  item.name = 'hi';
  item.value = 6;
});
pending…
map
range.map(function(item) {
  item.name = 'hi';
  item.value = 6;
});
pending…
while
var item,
  ln = range.length;

while (--ln > -1) {
  item = range[ln];
   item.name = 'hi';
  item.value = 6;
}
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