# What's the best way to store tiles' infos in a 2D table?

I need to know what's the less time / memory consuming way to store the infos about each tile in a 2D table. The first thing that came to my mind was creating a table holding the infos, and then storing in each tile the string or the number used to access these infos, like:

tilesTable = {
["sand"] = {
walkable = true,
image = "sand.png",
--stuff
},
["sea"] = {
walkable = false,
image = "sea.png" --...and so on
}
}

map = {{"sea", "sand", "sand"},
{"sea", "sea", "sand"},
{"sea", "sea", "sea"}}


And then accessing their informations like

print(tilesTable[map[x][y]].image)


And the same would be with number indexes.

But then I tought that when trying to access the infos for each tile, I am first accessing the _G table, then the map table, then the map[x] table, then using the result to access again the _G table and the tilesTable, so I tought to store in each position the reference to a global / local table, like

--[[local]] sand = {walkable = true, image = "sand.png"}
--[[local]] sea = {walkable = false, image = "sea.png"}

map = {{sea, sand, sand},
{sea, sea, sand},
{sea, sea, sea}}


So that then I just have to call

print(map[x][y].image)


Which i suspect being faster, especially if the table is stored as a local variable.

What should I do? Again, what is the way that is faster and consumes less memory?

• What you should always do when you suspect that one way of doing things is faster and consumes less memory but you are not sure about it: Measure it. – Philipp May 16 '16 at 21:01
• Agree w/ @Philipp about measuring. Also, a bit more info would help - storing data on 100 tiles is a very different thing than doing the same for millions of tiles. Finally, keep in mind increased speed often comes at the cost of increased memory use. – Pikalek May 17 '16 at 2:43
• The player will be able to explore as far as it's memory lets it explore. I don't have a dynamic load and unload chunks system yet but even if I had it avoiding wasting memory is always great. – user6245072 May 17 '16 at 4:38
• Also I know how to test how fast something is - but testing how much memory it takes it's a mistery for me. – user6245072 May 17 '16 at 4:39
• @user6245072 The quick and dirty way to measure memory consumption is to just check the task manager and look for how much memory the application consumes. The more accurate way would be material for a new question on stackoverflow.com. – Philipp May 17 '16 at 6:45