Angular forEach vs. Native for loop (v16)

Revision 16 of this benchmark created on


Preparation HTML

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

Setup

var dataset, i, retObj = {}, stats = [{
        "window": "lifetime",
        "source": "DX",
        "spend": "1642.81",
        "ideal_spend": "2989.86",
        "pace": "43",
        "impressions": "224929",
        "actions": "189",
        "cpm": "3.90",
        "ideal_cpm": "2.81",
        "cpa": "25.82",
        workLogs: [{
          id: 'item1',
          hours: [5, 3, 6, 2, 1]
    
        }, {
          id: 'item2',
    
          hours: [5, 3, 6, 2, 1]
    
        }]
      }, {
        "window": "yesterday",
        "source": "DX",
        "spend": "1634.57",
        "ideal_spend": "3276.75",
        "pace": "24",
        "impressions": "24771",
        "actions": "41",
        "cpm": "2.08",
        "ideal_cpm": "3.14",
        "cpa": "16.00",
        workLogs: [{
          id: 'item1',
    
          hours: [5, 3, 6, 2, 1]
    
        }, {
          id: 'item2',
    
          hours: [5, 3, 6, 2, 1]
    
        }]
      }];

Test runner

Ready to run.

Testing in
TestOps/sec
Angular forEach
angular.forEach(stats, function(dataset) {
  angular.forEach(stats.workLogs, function(workLog) {
    angular.forEach(stats.workLogs.hours, function(work) {
      if (dataset.window && dataset.window === 'lifetime') {
        delete dataset.window;
        retObj = dataset;
      }

    });

  });
});
ready
Native For
for (i = stats.length - 1; i >= 0; i--) {
  for (j = stats[i].workLogs.length - 1; j >= 0; j--) {
    for (k = stats[i].workLogs[j].hours - 1; k >= 0; k--) {
      if (stats[i].window && stats[i].window === 'lifetime') {
        delete stats[i].window;
        retObj = dataset;
        break;
      }
    }
  }
}
ready
Native forEach
stats.forEach(function(dataset) {
  workLogs = dataset.workLogs;
  workLogs.forEach(function(workLog) {
    works = workLog.hours;
    works.forEach(function(work) {
      if (dataset.window && dataset.window === 'lifetime') {
        delete dataset.window;
        retObj = dataset;
      }

    });

  });
});
ready

Revisions

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