Array Initialization
Initializing arrays in JavaScript is a crucial task, with many techniques to choose from and performance considerations to keep in mind. While there might not be a one-size-fits-all solution, there are a few options you might want to consider.Array() Constructor
The first thing you’d reach for would probably be theArray() constructor. Counterintuitively, this is probably the most problematic option to use on its own. While it works for any number of arguments to create an array with the given values, it falls short pretty much everywhere else.
The Problem with Empty Slots
Most of its problems stem from holes or “empty” values with which the resulting array is populated and how these are handled elsewhere.Array.from()
Array.from() is a static method that creates a new, shallow-copied Array instance from an array-like or iterable object. It is very useful for converting array-like objects (e.g. arguments, NodeList) or iterables (e.g. Set, Map, Generator) into actual arrays.
Creating Arrays with Length
Apart from that, it can easily be “tricked” into creating an array of a given length by passing an object with alength property. This is somewhat slow, but it works well and circumvents some of the problems of the Array() constructor.
With Mapping Function
Additionally, it allows you to pass a mapping function as a second argument, which is very useful for initializing arrays with values.Array.prototype.fill()
WhileArray.from() is quite flexible, using a mapping function to fill it with the same value isn’t particularly efficient. Array.prototype.fill() comes to fill this gap by allowing you to fill an existing array with the same value.
Array() constructor, as it allows you to fill the array with a value, instead of empty slots.
Array.prototype.map()
Array.from() allows for a mapping function via a second argument, but a lot of people think it’s hard to read. Additionally, there are a few edge cases, where having access to the array itself during mapping can be useful.
Advanced Mapping
Array.prototype.map() gives you this little bit of extra flexibility and readability if that’s what you’re concerned about.
Performance Recommendations
Performance might be a concern if this sort of operation is very common in your application, but overall none of these options are particularly slow. TheArray() constructor seems to be the fastest. That being said, if combined with Array.prototype.fill(), it can be the best option for initializing an array with a single value.
Recommended Helper Functions
Oddly enough, this performance advantage still holds even if you chain anArray.prototype.map() call afterwards to create dynamic values.
Summary
Best for static values
Best for static values
Use
Array(n).fill(value) for the best performance when filling with a single value.Best for dynamic values
Best for dynamic values
Use
Array(n).fill(null).map(fn) for creating arrays with dynamic values based on index.Best for readability
Best for readability
Use
Array.from({ length: n }, fn) for the most readable code with a mapping function.Best for conversion
Best for conversion
Use
Array.from(iterable) to convert array-like objects or iterables to arrays.