Lua table/list string cast/concat -


is there method more efficient simpler anonymous function use, when casting list (a table 1 level) string?

why asking this? have heard string concatenation on , on in lua memory inefficient because lua strings immutable objects have thrown garbage collector once used.

so anonymous function usage mean this:

local resources = {     ["bread"] = 20,     ["meat"] = 5,     ["something"] = 99 };  local function getthetext()     return "resources:\n"..         ( (function()              local s = "";              k, v in pairs(resources)                  s = s.."\n\t"..k..": "..v.." units.";              end              return s;         )())..         "\nmore awesome info...";    end end 

you know, time use a metafunction tostring on table because not going change, if use other anonymous tables not have option.

anyway love anonymous usage of functions so, not problem me.


is there more efficient not require declaring functions? , more or less efficient using metamethod?

local function getthetext()     local temp = {"resources:"}     k,v in pairs(resources)         temp[#temp + 1] = "\t"..k..": "..v.." units."      end      temp[#temp + 1] = "more awesome info..."      return table.concat(temp, "\n") end 

table.concat build strings efficiently , use less memory concatenating via s = s ...

this issue covered in pil


Comments