4 ms·
> Pre-allocating an array using new is measurably better for performance in some browsers. If you need to create a new array and populate it with values, then
by M1573RMU74710N 15y ago
> Pre-allocating an array using new is measurably better for performance in some browsers.
If you need to create a new array and populate it with values, then using new Array( size ), and then looping through it to populate it will be faster than using a literal or new Array() and then looping through it.
It should be noted though, that the difference is negligible unless you're working on creating and pre-populating a lot of very large arrays.
This situation is rare to begin with, and even then in most cases it's not a big difference.
In perhaps the most common case, creating an empty array, a literal is generally faster than (or equivalent with) using the Array constructor (at least, in tests I have done).
I would go with the syntax you believe is cleaner unless you have a demonstrable perf bottleneck....speed shouldn't even be brought up until then (unless you are trying to decide between two truly equal options, then picking the faster one is reasonable, but that's not the case here)