How to pre-allocate a dense array in Javascript?
arrays, javascript, performance
Solution
100000 is 1 past the pre-allocation threshold, 99999 still pre-allocates and as you can see it's much faster
http://jsperf.com/big-array-initialize/5
Problem
When using the `new Array(size)` ctor, if `size` is not a constant, JS seems to create a sparse array in some browsers (at least in Chrome), causing access to be much slower than when using the default ctor, as shown here. That is exactly the opposite of what I want: I pre-allocate an array of given size to avoid dynamic re-allocation and thereby improving performance. Is there any way to achieve that goal? Please note that this question is not about the ambiguity of the `new Array(size)` ctor. I posted a recommendation on that here.