Loop vs map vs forEach

JavaScript performance comparison

Revision 47 of this test case created by Jeremy Marzka

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var range = [];
  
  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
map spread
var result = range.map(item => ({
  ...item,
  ...{ name: 'hi', value: 6 }
}));
pending…
forEach es6
const res = new Array(range.length);

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

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

range.forEach(function(item, idx) {
  item.name = 'hi';
  item.value = 6;
  result[idx] = item;
});
pending…
map
var result = range.map(function(item) {
  item.name = 'hi';
  item.value = 6;
  return item;
});
pending…
forEach using push
var result = [];

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…

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