I "gathered some data" (actually, it was just personal experience) related to this. Look at this commit history.
As you can see, we started with Array(n)
+for
loop (probably because in "the old days" pre-allocating an array was faster than repeatedly push
ing values, otherwise the garbage-collector and the allocator would work too much).
Then we transitioned to []
+while (n--)
+push
. Then Array(n).fill().map()
. And finally Array.from({ length }, ...)
.
I was confused as to why this is a "hacky solution", because I thought { length }
was a named-arg bag. Then I read the docs, and realized the arg is supposed to be iterable or "array-like", and { length }
is neither but it still works, because it has the minimal requirement to be considered "array-like" (the length
prop). The mapping fn is also discarding both args (value and index), which makes it a "filler" rather than a "mapper"