The purpose of preallocating and using new Array(n) in addition to good readability is to pre-reserve the memory stack . Then, when we start using the stack, we avoid all the problems of reallocating memory and reallocating and copying items .
I do not know about the implementation of engines and processing for automatic distribution. And how optimized it is. But usually they allocate a little more than what they initialized, then when we reach the end , they redistribute with a large size (following some policy). And so on. So use push, unshift ... etc. May greatly affect performance .
Of course, I did not say how we can optimize and use new Array(n) ?
The answer is simple. It manages the stack independently , as in regular arrays in c or c ++ . The goal is to allow the engines to activate the implemented optimization mechanisms there. Which basically solves the issue of creating an optimized constant binary machine code (in many cases, the performance in such blocks will be equivalent to or close to the performance of the code written in c / c ++). What is the work of the jit compiler (just in time) and its optimization kit .
In our case, all this will translate to the use of normal arrays instead of dynamic vectors . And the issue of freeing, redistributing, and copying will also be fixed .
To better understand, I propose this golden article that examines various ways to optimize arrays.
gold article .
And this is a video that explains and demonstrates how v8 performs some optimization and how important little things are. https://www.youtube.com/watch?v=p-iiEDtpy6I
Bonus Note
And finally, remember that you do not need to optimize until it matters, and it matters .
If you are working in a regular application, without much benefit or importance, it will not matter (a good example is reactive structures, such as reaction, redistribution always exists, (immutable elements), but it does not matter, and In case we need to optimize for some reason, we can always do this, and we can apply the above, for example, with some hooks of side effects (all such frames provide simplicity and responsiveness against senseless optimization. With open doors for I am optimizing if necessary.).
And an example, and even in environments where you might think about optimization, is when you have a large dataset or element processing, which involves repetition and processing. An example would be creating a very complex chart module. You can work with it separately from the platform and optimize everything you can. And then wrap it up. We can also mention games, complex treatment and calculations ... etc.
Actual testing and measurement (using your development tool) is very important [this is what actually allows us to notice the difference].
And if some tips are easy to implement, why not make them part of your style and trend.