In many programming languages an array can reserve a number of elements which allocates some amount of memory in advance to improve performance when adding elements to an array. Is there some equivalent in Javascript to do that kind of optimisation?
In many programming languages an array can reserve a number of elements which allocates some amount of memory in advance to improve performance when adding elements to an array. Is there some equivalent in Javascript to do that kind of optimisation?
Yes, you can use the array constructor with a single argument:
const arr = new Array(10); // now has 10 empty slots
Read more here. Both V8 and Spidermonkey (Chrome and Firefox's engines, respectively) receive a noticeable performance boost when preallocating the array. I have not tested on other engines.