Angular forEach vs. Native for loop (v58)

Revision 58 of this benchmark created by ViBiOh on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js"></script>

Setup

var dataset, retObj = undefined, stats = [];
    for (var nb = 0; nb < 1000; ++nb) {
      stats.push({
        id: nb,
        label: 'label_for_' + nb
      });
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Angular forEach
angular.forEach(stats, function(dataset) {
  if (retObj === undefined && dataset.label === 'label_for_500') {
    retObj = dataset;
  }
});
ready
Native For
var i = 0;
for (var j = stats.length; i < j && stats[i].label !== 'label_for_500'; i+=1) {
  continue;
}
if (i < j) {
  retObj = stats[i];
}
ready
Native forEach
stats.forEach(function(dataset) {
  if (retObj === undefined && dataset.label === 'label_for_500') {
    retObj = dataset;
  }
})
ready
Some
stats.some(function(dataset) {
  if (dataset.label === 'label_for_500') {
    retObj = dataset;
    return true;
  }
  return false;
});
ready
Native for reverse
for (var i = stats.length - 1; i >= 0 && stats[i].label !== 'label_for_500'; i -= 1) {
  continue;
}
if (i >= 0) {
  retObj = stats[i];
}
ready

Revisions

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