January 2018
Intermediate to advanced
332 pages
7h 36m
English
We can do a deep-down dive into the for loop, similar to what we did with the if condition earlier (https://www.ecma-international.org/ecma-262/5.1/#sec-12.6.3), but there are easier and more obvious optimizations which can be applied when it comes to loops. Simple changes can drastically affect the quality and performance of the code; consider this for example:
for(var i = 0; i < arr.length; i++) { // logic}
The preceding code can be changed as follows:
var len = arr.length;for(var i = 0; i < len; i++) { // logic}
What is even better is to run the loops in reverse, which is even faster than what we have seen previously:
var len = arr.length;for(var i = len; i >= 0; i--) { // logic}
Read now
Unlock full access