Tables¶
Imply Array Indices¶
Other languages don't allow declaring an array with explicit indices. Unless the keys have important meaning that needs to be made clear to the reader, they should be implied.
Dereferencing¶
Prefer object access for constant keys, and array access for non-constant keys¶
Extract duplicate table dereferences into local variables¶
This is both a readability and performance boost
Avoid table.insert()¶
It has horrible performance. It should only be used if needing to insert into an array at a specific index that is not the last index.
Inserting at the end of a table¶
Inserting/Overwriting a given key¶
Use numeric for loops when iterating over an array¶
This is a performance boost
Maintain your own array size variable¶
There is a significant performance difference for large arrays as #array is an O(n) operation. Note that sometimes iterating through the the entire array to find the size is preferable, but a common pattern of starting with an empty array and populating it in a loop should use an array size variable.