Make it so you can initialize arrays of a certain length
See original GitHub issueIn regular TS/JS you can initialize arrays of a predefined length. This is useful for performance critical code where one can benefit from not resizing the dynamically-sized array several times. The single parameter is nice sugar we could easily integrate into the transpiler.
const strings = new Array<string>(12);
local strings = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:7 (5 by maintainers)
Top Results From Across the Web
How to Declare and Initialize an Array in Java - Stack Abuse
To use the array, we can initialize it with the new keyword, followed by the data type of our array, and rectangular brackets...
Read more >How to initialize an array's length in JavaScript? - Stack Overflow
The number of objects in the array is user-defined, so I was letting the user pick a number, then initializing the array with...
Read more >Creating and filling Arrays of arbitrary lengths in JavaScript
One common way of creating an Array with a given length, is to use the Array constructor: const LEN = 3; const arr...
Read more >How To Initialize An Array In Java With Values - Xperti
To initialize an Array with default values in Java, the new keyword is used with the data type of the Array The size...
Read more >Declare, Create & Initialize An Array In Java
Answer: Yes. Arrays are static in Java and you declare an array with a specified size. Once this size is specified, you cannot...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Firstly, avoiding 20 reallocations at run-time involving hundreds of thousands of elements being deleted and copied is more than “marginally faster”, although that is not the use-case or reason JS and Lua have shortcuts for these.
Lua has a built-in optimization for this that can initialize up to 50 nil values in an array at a time. That would probably be the maximum number of
nilvalues you would ever want to put into a script in-line (or maybe 100 nil values, which would be 4 bytecode instructions). For insane cases where someone wants to allocate a million entries, we could either fallback on a for loop or just let Lua run that dynamically.I think it should be noted that in regular JS/TS, presizing arrays does not actually allocate anything (all it does it set the
lengthproperty) and in fact has the inverse effect and reduces the performance of the array significantly for as long as the object exists (even if it’s filled in later)