Best Practices
Learn the best practices for writing code in LUA
Tables and Arrays
When working with structured data
, use tables with named keys
for better performance
, readability
, and maintainability
Arrays are good
for lists of simple values, but tables are better
for complex structured data
examples
to demonstrate faster data retrieval using tables versus arrays Arrays have their own purpose and aren’t inherently bad; they just have different use cases
Choosing Between Tables and Arrays
Tables are faster
than arrays for data lookups because they use a hash map internally, providing constant time lookups, making them much more efficient
, especially for larger datasets
Arrays are slower in lookups
due to linear time complexity , as they require looping through each element
until the value is found
for i
loops are generally faster than ipairs
or pairs
only arrays work with ipairs or for i loopsEfficient Data Structuring
When working with structured data, use named keys
in tables to improve readability
, clarity
, and performance
Named keys allow for easier understanding
of what each field represents, and they also enable faster lookups
Arrays with indexed values are harder to read and maintain
, and lookups are slower
, especially as the size of the data grows.