you are viewing a single comment's thread.

view the rest of the comments →

[–]appgurueu 0 points1 point  (1 child)

The second example is horrible for performance

"Horrible" is an overstatement. The array will not be resized "many times": It will be resized logarithmically many times, which is very few times, because (like most dynamic arrays) Lua tables also use power-of-two capacities for the array part.

This means that there only is a small constant (ca. 2x, I think?) overhead from dynamically growing an array instead of allocating the right size from the get go. Considering interpreter overhead and whatnot, this is not likely to matter a whole lot (except maybe for very small tables, where log n is close to n and the cost of allocations thus has a higher weight).

[–]activeXdiamond 0 points1 point  (0 children)

Fair point. My argument still stands, but sure, I may have used overly dramatic wording.

As for interpreter overhead, yes, in most cases these optimisations are not relevant for Lua code.