It is necessary to do an array traversal.

And is it worth writing [1,2,3].forEach(async function(e){console.log(e);}); ?

Or is it worth writing this for (let e of [1,2,3]) {console.log(e);} ?

Already from all variety eyes run up. I do not see the difference. I would like faster code execution.

  • Хотелось бы более быстрого выполнения кода - on arrays of a billion elements there can and will be a difference .... but on small ones that are used in the frontend - no difference - Aleksey Shimansky
  • This is more a matter of code style than performance, if the function is short (1 operation for example) I often write in the first way. If it's a long one, I write through a cycle. - Dmitry Polyanin
  • Well, type regexpData.forEach(async function(e){if( e.test(str) ) console.log(e.exec(str));}); (str = let change in the upper block) it is easy for you to make out what's what - Alex TvoyTruba
  • Well, the quickest way is to go through indexes, as usual for(let i=0;i<arr.length;i++){console.log(arr[i]);} . - Stepan Kasyanenko

2 answers 2

The fastest way is bypassing the index.

An example on jsfiddle .

 function fillArray(cnt) { let res = []; for (let i = 0; i < cnt; i++) { res.push(i); } return res; } var arr = fillArray(5000000); var t = null; t = Date.now(); arr.forEach(async function(e, i) { arr[i] += 1; }); console.log('time forEach async', Date.now() - t); t = Date.now(); arr.forEach(function(e, i) { arr[i] += 1; }); console.log('time forEach', Date.now() - t); t = Date.now(); for (let e of arr) { e += 1; // здесь мы значение массива не меняем } console.log('time let of', Date.now() - t); t = Date.now(); for (let i = 0; i < arr.length; i++) { arr[i] += 1; } console.log('time for with index', Date.now() - t); 

Update

In fact, the test turned out to be quite controversial, because in the loop we change the array, and this operation can have different execution times in terms of speed.

Therefore, it would be more logical to start an empty loop, with no action inside.

As pointed out in the @Grundy comments, Edge is faster.

An example on an empty jsfiddle loop.

 function fillArray(cnt) { let res = []; for (let i = 0; i < cnt; i++) { res.push(i); } return res; } var arr = fillArray(5000000); var t = null; t = Date.now(); arr.forEach(function(e, i) { }); console.log('time forEach', Date.now() - t); t = Date.now(); for (let e of arr) { } console.log('time let of', Date.now() - t); t = Date.now(); var len = arr.length; for (let i = 0; i < len; i++) { } console.log('time for with index', Date.now() - t); 

The most interesting thing to check results in Edge .

In the code above, even in Edge , index crawling is faster.

But there are 3 interesting nuances:

  • if we remove the let len = arr.length; , the code will work slower.
  • if we replace let i = 0 with var i = 0 , the code will run slower.
  • if we add the array change code to all cycles, the index traversal will become slower.

I think we can conclude that Edge has poor code optimization.

  • eee !!! rooted for. Oldies showed these new features. - user208916
  • It’s good, though I thought that both forics would work at about the same speed. And then I remembered that this is not a compiled language. However, depending on the browser / platform, it can still jump a bit - selya
  • In general, your test is wrong. In for, you also need to call a function, and you simply refer to the element of the array! Here is the correct test: jsperf.com/for-vs-foreach-vs-for-of2 - user208916
  • By the way, with this test. My browser gives out 178 51 13 10 respectively, and node.js 172 51 12 14 , so the index is only conditionally faster ... Noda and chrome are the last. PS I drove at the node several times, at the node it jumps very strongly. +- 5 for each of the four tests, with that for each independently. - selya
  • @Hipster hope this was sarcasm on your part. Why should I call a function in for ? - Stepan Kasyanenko

IMHO is better to use just for both in terms of readability and in terms of performance https://jsperf.com/for-vs-foreach-vs-for-of2