Javascript Efficiency: 'for' vs 'forEach'

What is the current standard in 2017 in Javascript for for () loops vs .forEach.

I currently work through the Colt Steeles "Web Dev Bootcamp" on Udemy, and he endorses forEach on for in his teachings. However, I searched for various things during the course work exercises, and I find more and more recommendations for using for -loop rather than forEach . Most people seem to think that the for loop is more efficient.

Whether this is what has changed since the writing of the course (around 2015) or really their pros and cons for each of them, which will be studied with great experience.

Any advice is appreciated.

+36
javascript loops foreach for-loop
source share
2 answers

per

for loops are much more efficient. This is a looping construct specially designed for iteration when the condition is satisfied, and at the same time it offers a step-by-step mechanism (usually to increase the iterator). Example:

 for (var i=0, n=arr.length; i < n; ++i ) { ... } 

This does not mean that for loops will always be more efficient, just JS engines and browsers have optimized them for this. Over the years, compromises have been made regarding which cyclic design is more efficient (for example, to reduce time, reverse time, etc.) - different browsers and JS engines have their own implementations that offer different methodologies for obtaining the same results. As browsers are further optimized to meet performance requirements, theoretically [].forEach can be implemented in such a way that it is faster or comparable to [].forEach for .

Benefits:

  • effective
  • early termination of the cycle (with honors break and continue )
  • state management ( i<n can be anything and is not related to the size of the array)
  • Overview variables ( var i leaf i is available after the loop ends)

for each

.forEach are methods that basically .forEach over arrays (also other enumerated ones, such as Map and Set objects). They are newer and provide code that is subjectively easier to read. Example:

 [].forEach((val, index)=>{ ... }); 

Benefits:

  • does not require setting variables (iterates over each element of the array)
  • functions / arrow-functions limits the variable to a block
    In the above example, val will be the parameter of the newly created function. Thus, any variables called val before the loop will contain their values ​​after it ends.
  • subjectively more convenient to maintain, as it may be easier to determine what the code does - it iterates over the enumerable; while a for loop can be used for any number of cyclic schemes

Performance

Productivity is a complex topic that usually requires some experience when it comes to thinking or approaching. In order to determine in advance (during development) how much optimization may be required, a programmer must have a good idea of ​​past experience in solving a problem, as well as a good understanding of possible solutions.

Using jQuery in some cases can be too slow (an experienced developer can know this), while in other cases it is not a problem, in this case the cross-browser compatibility of the library and the simplicity of other functions (for example, AJAX, event handling) were worth it would have time for development (and maintenance).

Another example: if performance and optimization were everything, there would be no code other than a machine or assembly. Obviously, this is not so, since there are many different languages ​​of high level and low level, each of which has its own compromises. These trade-offs include, but are not limited to, specialization, simplicity and speed of development, simplicity and speed of service, optimized code, error-free code, etc.

An approach

If you do not have a clear understanding that optimized code will be required for something, then as a rule, it is recommended that you first write the code you serve. From there, you can check and determine what needs more attention when it is required.

However, some obvious optimizations should be part of the general practice and should not require any thought. For example, consider the following loop:

 for (var i=0; i < arr.length; ++i ){} 

For each iteration of the JavaScript loop arr.length , arr.length for key search operations in each loop. There is no reason why this should not be:

 for (var i=0, n=arr.length; i < n; ++i){} 

This does the same thing, but arr.length only extracts arr.length once, arr.length variable and optimizing your code.

+86
source share
+4
source share

All Articles