Lua Table Access Efficiency

I have a question about accessing data in a Lua table.
Let's say there is a large Lua table, for example:

 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 blocks, which blockIdx, for example 38.
Therefore, usually I would use forto search for a block:

for k,v in pairs(tbl) do
     if v.blockIdx == 38 then
        blahFunction(v)
     end
end

But I do not think this is a good idea, especially for a large table.
Therefore, I modify the table a little:

 tbl = {
         [5] = { 
           key1 = "val1",
           key2 = "val2",
           ...
         },
         [30] = {
           key1 = "val11",
           key2 = "val12",
           ...
         },
         [83] = {
           key1 = "val21",
           key2 = "val22",
           ...
         },
         ...
       }

Then I can easily access my block with a single line:

blahFunction(tbl[38])

, : - , ?
, tbl[38] for Lua?
C/++ [ ] for,
, .

+4
1

, .

Lua -, , . , , , -. C/++, , .

, , , , .

+4

Source: https://habr.com/ru/post/1537814/


All Articles