i have an array of objects all with the properties name,id,itemType and assetType. The problem is that i have 30000 of these objects, would it be better to structure each object like this {name,id,itemType,assetType} ? my guess is that it helps the game load faster, it also helps me to reduce the amount of lines in my module
Well it is true that standard arrays are much more performant that dictionaries. If you are willing to increase complexity for better performance, go ahead.
Dictionaries (hash maps) are quicker to access/assign to but they come with the drawback of not being compatible with the unary length operator (#) and a majority of the table library functions.
local Hash = {}
local Array = {}
for Value = 1, 1000000 do
table.insert(Array, Value)
end
for Value = 1, 1000000 do
Hash[tostring(Value)] = true
end
local Clock1 = os.clock()
print(Array[500000])
print(os.clock() - Clock1) --0.000085
local Clock2 = os.clock()
print(Hash["500000"])
print(os.clock() - Clock2) --0.000028
The difference however is essentially negligible, so one should use whatever is most contextually appropriate.