I tried next code (it shows similar results in Google Chrome and nodejs):
var t = new Array(200000); console.time(\'wtf\'); for (var i = 0; i < 200000; ++i) {
As of summer 2020, v8 has changed significantly.
It used to default to dictionary-mode if initial size > some_number_that_is_roughly_10k. That has changed.
Judging from the Array constructor code, the SetLengthWouldNormalize function and the kMaxFastArrayLength constant, it can now support an almost arbitrarily large amount (currently capped at 32 million) of elements before resorting to dictionary mode.
Note, however that there are many more considerations at play now, as V8 optimization has become ever more complicated. This official blog post from 2017 explains that arrays can distinguish between 21 different kinds of arrays (or rather, array element kinds), and that - to quote:
"each of which comes with its own set of possible optimizations"
I would strongly recommend:
As you probably already know, if you pre-allocate an array with > 10000 elements in Chrome or Node (or more generally, in V8), they fall back to dictionary mode, making things uber-slow.
Thanks to some of the comments in this thread, I was able to track things down to object.h's kInitialMaxFastElementArray.
I then used that information to file an issue in the v8 repository which is now starting to gain some traction, but it will still take a while. And I quote:
I hope we'll be able to do this work eventually. But it's still probably a ways away.