Native map vs. forEach appending vs. array looping

Benchmark created by Long Ouyang on


Preparation HTML

<script>
  var g = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27];
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Map (native)
var h = g.map(function(x) {
 return x + 2
});
ready
forEach (native)
var h = [];
g.forEach(function(x) {
 h.push(x + 2);
})
ready
Loop
var h = [];
for (var i = 0, len = g.length; i < len; i++) {
 h.push(g[i] + 2);
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.