forEach native vs. JS

JavaScript performance comparison

Test case created by Bryan Forbes

Preparation code

<script>
  var arr = (function() {
   var arr = [],
       i = 100;
  
   while (i--) {
    arr.push(Math.random());
   }
  
   return arr;
  })();
  
  var sum;
  
  function iterate(i) {
   sum += i;
  }
  
  function forEach(arr, callback, scope) {
   for (var i = 0, l = arr.length; i < l; i++) {
    callback.call(scope, arr[i], i, arr);
   }
  }
  
  function forEach5(arr, callback, scope) {
   for (var i = 0, l = arr.length; i < l; i++) {
    if (i in arr) {
     callback.call(scope, arr[i], i, arr);
    }
   }
  }
</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
native
sum = 0;
arr.forEach(iterate);
pending…
JS (non-ES5)
sum = 0;
forEach(arr, iterate);
pending…
JS (ES5)
sum = 0;
forEach5(arr, iterate);
pending…

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

Compare results of other browsers

0 Comments