title | description | mode |
---|---|---|
Best Practices |
Learn the best practices for writing code in LUA |
wide |
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
These are just 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
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 loops
local job = "police"
local table = {police = true, doctor = true}
local hasJob = table[job]
-- the larger the array the slower it gets
local array = {"police", "doctor"}
local job = "police"
local hasJob = false
for i = 1, #array do
if array[i] == job then
hasJob = true
break
end
end
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.
local table = {
{grade = 0, job = "police"},
{grade = 0, job = "doctor"}
}
local job = "police"
local hasJob = false
for i = 1, #table do
if table[i].job == job then
hasJob = true
break
end
end
local array = {
{ 0, "police"},
{ 0, "doctor"}
}
local job = "police"
local hasJob = false
for i = 1, #array do
for j = 1, #array[i] do
if array[i][j] == job then
hasJob = true
break
end
end
if hasJob then break end
end