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.