Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Chrome 123
Windows
Desktop
9 months ago
Test name Executions per second
forEach 5235746.0 Ops/sec
for 13095759.0 Ops/sec
for optimized 13159533.0 Ops/sec
reduce 14091031.0 Ops/sec
while 9659842.0 Ops/sec
for in 471848.3 Ops/sec
for of 3779423.2 Ops/sec
for of babel 1799.6 Ops/sec
HTML Preparation code:
AخA
 
1
<script>
2
    Benchmark.prototype.setup = function() {
3
        var testData = [];
4
        for (var i = 0; i < 100; i++) {
5
            testData.push(i);
6
        }
7
    };
8
</script>
Tests:
  • forEach

     
    var res = 0;
    testData.forEach(function(x) {
      res += x;
    });
  • for

     
    var res = 0;
    for (var i = 0; i < testData.length; i++) {
      res += testData[i];
    }
  • for optimized

     
    var res = 0;
    for (var i = 0, len = testData.length; i < len; i++) {
      res += testData[i];
    }
  • reduce

     
    var res = testData.reduce(function(sum, x) {
      return sum + x;
    }, 0);
  • while

     
    var res = 0;
    var i = testData.length;
    while (i--) {
        res += testData[i];
    }
  • for in

     
    var res = 0;
    for (var data in testData) {
      res += testData[i];
    }
  • for of

     
    var res = 0;
    for (var data of testData) {
      res += testData[i];
    }
  • for of babel

    x
     
    var res = 0;
    var _iteratorNormalCompletion = true;
    var _didIteratorError = false;
    var _iteratorError = undefined;
    try {
      for (var _iterator = testData[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
        var value = _step.value;
        console.log(value);
      }
    } catch (err) {
      _didIteratorError = true;
      _iteratorError = err;
    } finally {
      try {
        res += testData[i];
        if (!_iteratorNormalCompletion && _iterator.return) {
          _iterator.return();
        }
      } finally {
        if (_didIteratorError) {
          throw _iteratorError;
        }
      }
    }