Although arrays are objects in Javascript, it is not recommended to use for in loops to traverse arrays. In fact, there are many reasons to prevent us from using for in loops on arrays.
Because the for in loop will enumerate all properties on the prototype chain, and the only way to stop it is to use hasOwnProperty to judge, this will be much slower than a normal for loop.
Traverse
For best performance, the best way to iterate over an array is to use a classic for loop.
An extra trick here is to cache the length of the array via l = list.length.
Even though the length property is defined on the array itself, there is still overhead on each iteration of the loop. Although the latest Javascript engine may have performance optimizations for this situation, there is no guarantee that your Javascript code will always run in this browser.
In fact, loops with uncached lengths are much slower than loops with cached lengths.
length attribute
Although the length property only returns the number of elements in the array through the getter method, the array can be truncated through the setter method.
Assigning a smaller number to the length property will truncate the array, while assigning a larger number will not truncate the array.
Summary
For optimal performance, it is recommended to use a for loop instead of a for in loop and cache the length property.
There are also array objects that do not have any methods, only one unique attribute length. String objects have length methods~~