for i,v in ipairs(tableA) do
table.remove(tableA, i)
end
doesn’t work, and I just figured out why. The indices of all other vales shift as an index is removed.
My question is, what do I replace it with?
for i,v in ipairs(tableA) do
table.remove(tableA, 1)
end
or
for i,v in ipairs(tableA) do
tableA[i] = nil
end
My problem with the second one is that the table will increase in size forever as I keep inserting into it, and my problem with the first one is that it seems a bit stupid.
But if you think the first one’s alright then I will use that.
Just to expand on this, use table.clear(tableA) instead of tableA = {} if you intend to use tableA again, as the former approach caches the memory allocated to the array part of the table allowing for it to be reused more efficiently.
task.wait(3)
local t1 = os.clock()
local tables1 = {1, 2, 3}
tables1 = {}
tables1 = {4, 5, 6}
local t2 = os.clock()
local tables2 = {1, 2, 3}
table.clear(tables2)
tables2 = {4, 5, 6}
local t3 = os.clock()
print(t2 - t1)
print(t3 - t2)
It may be hard to visualize these numbers but the second option is almost twice as performant as the first option.