Here is a small snippet that was measured using jsperf sit
Array(n) is a lot slower than
In the background there are different types of arrays though for a developer it looks like there is only one Array type.
For the sake of scope, two types can be discussed here.
- Holey element type
- Packed element type
One of it is a
delete operation. Another case is,
const arr = ; arr = 10;
- To check if the index is in the range (zero to arr.length)
- return value if hasOwnProperty of that index is true (as array is stored in exact object format with index as key)
- To check if the index is in the range
- hasOwnProperty of that index
- hasOwnProperty of Array.prototype
- hasOwnProperty of Object.prototype
- and the same with any/all extended entities
That's the reason, any operation on holey array takes several more steps than packed array causing performance difference.
Thanks for reading!