I read the following from w3schools here
Bad Code:
for (i = 0; i < arr.length; i++) {
Better Code:
l = arr.length;
for (i = 0; i < l; i++) {
I was taught C++ and Java in school and I never learnt that this is something we need to consider for efficiency. Isn't getting the size of array just a O(1) operation? Is this optimization really necessary and is it really what people do in industry?
EDIT:
Given that the array's size won't change.
arr.lengthmust be recalculated each time you pass through the loop. For small loops, it's completely unnoticeable.