Fast Array Foreach

JavaScript performance comparison

Revision 32 of this test case created by

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
    // Populate the base array
    var arr = [], source=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
    for (var i = 0; i < 1000; i++) {
      arr[i] = i;
    }
  
    function someFn(i) {
      var length = 35,
      i,
      rString = '';
  
      for (i = 0; i < length; i+= 1) {
        rString += source[Math.floor(Math.random() * source.length)];
      }
      return rString;
    }

};
</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
Array.ForEach
arr.forEach(someFn)
pending…
For
for (var i = 0, len = arr.length; i < len; i++) {
  someFn(arr[i]);
}
pending…
For in
for (var i in arr) {
  someFn(arr[i]);
}
pending…
Lodash
_.each(arr, someFn)
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