Iterators performance (v4)

Revision 4 of this benchmark created on


Description

Going through each element of an array. Testing the performance of different methods.

Based on this set of tests: http://jsperf.com/for-vs-foreach/159

Preparation HTML

<!-- jQuery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>

<!-- Underscore -->
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js">
</script>
<script>
  var underscore = _.noConflict();
</script>


<!-- LoDash -->
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js">
</script>
<script>
  var lodash = _.noConflict();
</script>

<!-- Lazy.js -->
<script src='//rawgithub.com/dtao/lazy.js/master/lazy.js'>
</script>

<!-- End preparation -->

Setup

var i,
      value,
      length,
      values = [],
      sum = 0,
      context = values;
    
    
    for (i = 0; i < 10000; i++) {
      values[i] = Math.random();
    }

Teardown


    i = 0;
    value = 0;
    length = 0;
    values = [];
    sum = 0;
  

Test runner

Ready to run.

Testing in
TestOps/sec
Native for
for (i = 0; i < values.length; i++) {
  sum += values[i];
}
ready
Native forEach
values.forEach(function(val) {
  sum += val;
});
ready
Native for, reverse
for (i = values.length - 1; i >= 0; i--) {
  sum += values[i];
}
ready
jQuery each
jQuery.each(values, function(i, val) {
  sum += val;
});
ready
Underscore each
underscore.each(values, function(val) {
  sum += val;
});
ready
Lodash each
lodash.each(values, function(val) {
  sum += val;
});
ready
Lazy each
Lazy(values).each(function(val) {
  sum += val;
});
ready
Native while
var i = values.lenght;
while (i--) {
  sum += values[i];
}
ready
Native while edit
var i = values.lenght;
while (i) {
  sum += values[i];
i--;
}
ready

Revisions

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