In the first code of an length
array (or a massively similar collection) is calculated only once , and it is cached . Thus, the length is not recalculated for each iteration.
While in the second code, the length is calculated per iteration .
We can say that caching lengths will be slightly faster than recalculating lengths. This difference will be very small so that you can neglect this for smaller arrays. But for a huge array, the difference can be significant.
.
, .
for (var i = 0; i < data.length; i++) {
// Useful when the data length is altered in here
}