Is there an equivalent method in Javascript arrays or ArrayList to Java's ensureCapacity? I am translating a certain Java code containing this method to Javascript, and couldn't find any equivalent to it. Thanks in advance
2 Answers
It is possible to initialize an array with a specific size:
var array = new Array(n);
If your array has already been initialized you can, as mentioned in comments, set the length of the array using the length property:
array.length = n;
However, the performance gains, if any, seems negligble. Here is a thread discussing this.
3 Comments
CodingIntrigue
And also set it via
array.lengthelbereth
So, for instance, if I am using it this way: public Path(int initialCapacity) { tsIindexes.ensureCapacity(initialCapacity); tsJindexes.ensureCapacity(initialCapacity); } would setting it with length amount to the same thing as done here?
Frederik Wordenskjold
Yeah, it would. But as mentioned, it is probably not necessary.
.length, but it doesn't make much difference.