Native map vs. forEach appending vs. array looping (v20)

Revision 20 of this benchmark created by xadn 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>

Setup

var mapper = function(x) {
      return x + 2;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Map (native)
var h = g.map(mapper);
ready
forEach (native)
var h = [];
g.forEach(function(x) {
 h.push(x + 2);
})
ready
Loop
var h = [];
h.length = g.length;

for (var i = 0, len = g.length; i < len; i++) {
 h[i] = g[i] + 2;
}
ready

Revisions

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