for vs foreach vs yolo

Edit on GitHub

Yolo

Setup

var count = 1000 * 1000
var data = [];

do {
  data.push(count);
} while(--count);

Tests

  • for

    var length = data.length;
    
    for(var i=0;i<length;i++){
      data[i]++;
    }
  • foreach

    data.forEach(function(element, index) {
       data[index]++;
    });
  • yolo

    data.forEach(function(element, index) {
       element++;
    });