Does referencing a table take the same memory as creating a new one?

Hello,

I wanted to know if in term of memory there was a difference between doing those two.

local Table = { 
     1 = "potato",
     2 = "tomato"
}
local RefTable = Table

-- or

local Table1 = { 
     1 = "potato",
     2 = "tomato"
}

local Table2 = { 
     1 = "potato",
     2 = "tomato"
}

Thx for your time !

2 Likes

RefTable points to Table.

Here you are just making a copy of the first.

1 Like

But does pointing a table means it take the same memory as if the table was existing twice ?

The variables both point to the same exact table in memory. Table has 2 references.

1 Like

Doing:

print(RefTable.1)

will do the same that

print(Table.1)

So the first one takes less memory than the second one if im right. So for example i could reference the table 10X times but itd still be 1 table in memory

Here’s a good test pulled from Basics of Basic Optimization

local function TestFunction()
	--Insert the code to tesst
end

local Start = tick() --Start time in seconds
for i = 1, 1000 do --For 1,000 times...
	TestFunction() --Call TestFunction
End
print(tick()-Start) --End time in seconds
1 Like

Yes, when you refer to a table, you’re not creating a new one, you’re just creating a local value that refers to a table, has I said, RefTable will be the same that Table.

1 Like

Alright ! Thx bois :slight_smile:

1 Like