Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36
Chrome 117
Mac OS X 10.15.7
Desktop
one year ago
Test name Executions per second
forEach 7116.5 Ops/sec
for..of 7447.7 Ops/sec
Script Preparation code:
x
 
   var arr_num = [];
   for (var i = 0; i < 1000; i++) {
       arr_num.push(i);
   }
   function numFn(i) {
       i = i * 3 * 8;
   }
   var arr_str = [];
   for (var i = 0; i < 1000; i++) {
       arr_str.push((Math.random() + 1).toString(36).substring(7));
   }
   function strFn(i) {
       i = i + '_';
   }
   var arr_obj = [];
   for (var i = 0; i < 1000; i++) {
       arr_obj.push({
           [(Math.random() + 1).toString(36).substring(7)]: true
       });
   }
   function objFn(i) {
       i.aaa = false;
   }
Tests:
  • forEach

     
    arr_num.forEach((item) => {
      numFn(item);
    });
    arr_str.forEach((item) => {
      strFn(item);
    });
    arr_obj.forEach((item) => {
      objFn(item);
    });
  • for..of

     
    for (const element of arr_num) {
      numFn(element);
    }
    for (const element of arr_str) {
      strFn(element);
    }
    for (const element of arr_obj) {
      objFn(element);
    }