DEV Community

Discussion on: Populating a pre-allocated array slower than a pushing to a regular array?

Collapse
 
henryjw profile image
Henry Williams

The JavaScript engine (as an optimization) uses actual typed arrays behind the scenes, this will not be the case if you use arrays with holes (empty slots i.e the length element is disproportional to the amount of properties (elements) in the array).

If I understand this correctly, does this mean that new Array(1000) doesn't actually allocate a typed array under the hood?

Collapse
 
mse99 profile image
Mohamed Edrah

It probably does allocate some sort of array or object behind the scene, It won't be as performant because the runtime will have to do extra work in order to get or set elements, but to answer your question, no allocation does happen one way or another the runtime will create a data structure to represent your program's data.