I have a question about accessing data in a Lua table.
Say, there is a large Lua table like following:
tbl = {
{
blockIdx = 5,
key1 = "val1",
key2 = "val2",
...
},
{
blockIdx = 30,
key1 = "val11",
key2 = "val12",
...
},
{
blockIdx = 83,
key1 = "val21",
key2 = "val22",
...
},
...
}
And now I want to find one of the block that blockIdx is , for example, 38.
So normally, I would like to use for to find the block:
for k,v in pairs(tbl) do
if v.blockIdx == 38 then
blahFunction(v)
end
end
But I don't think it is a good idea especially for large table.
So I modify the table a bit:
tbl = {
[5] = {
key1 = "val1",
key2 = "val2",
...
},
[30] = {
key1 = "val11",
key2 = "val12",
...
},
[83] = {
key1 = "val21",
key2 = "val22",
...
},
...
}
Then I can easily access my block with one line:
blahFunction(tbl[38])
So my question is, is there any performance different between two method?
Maybe doing tbl[38] actually did a for loop inside Lua?
Or just like an array in C/C++ we can direct access memory using [ ] without for loop,
witch obviously has much better performance.