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

Revision 4 of this benchmark created 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
Loop with sparse check
//based on v8's implementation
//http://code.google.com/p/v8/source/browse/trunk/src/array.js#1219
var length = g.length;
var h = [];
var add2 = function(x){ return x + 2; }; 

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

Revisions

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