DEV Community

Discussion on: 13 useful JavaScript array tips and tricks you should know

Collapse
 
jamesthomson profile image
James Thomson

FYI, #4 "Empty an array" will create a holey array. It's better to assign the value a new empty array [] rather than mutate the length. More on element kinds: v8.dev/blog/elements-kinds

Collapse
 
antonioyon profile image
Antonio Yon

Why would setting the length of an array to 0 make it holey? I would understand if you were to set the length to a value greater than its content, it would create a HOLEY_ELEMENTS. Truncating should not have that same problem.

I would think the engine would continue to optimize for a PACKED_ELEMENTS Array if length were set to a value less than its current content, including 0.