question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make it so you can initialize arrays of a certain length

See original GitHub issue

In 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:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
Validarkcommented, Mar 24, 2019

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 nil values 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.

0reactions
evaeracommented, Mar 25, 2019

I think it should be noted that in regular JS/TS, presizing arrays does not actually allocate anything (all it does it set the length property) 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)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found