The Table article is really confusing. I just clicked on it on accident and started reading it. I’m just going to quote some of the confusing things.
Why NumberValue and StringValue?? Those are objects. This should just say number and string instead. It even links to the NumberValue and StringValue objects. That’s super confusing.
What? No, it’s the # operator. And it counts the number of integer keys that are continual starting at 1. It has nothing to do with NumberValues and has nothing to do with *.
No! ipairs is very slow compared to pairs (in the version of Lua Roblox is using). And it gives no information as to why this is being stated. Note that this is the first time it even talks about iterating over an array. It never even mentioned pairs before, and it never talked about a normal numeric for loop iteration.
Again, no.
That’s great, but it literally ends there. What are these manipulation functions? There’s no information and no links. It’s a mystery.
The rest of the article talks about using tables as dictionaries & seems to make sense.
I don’t know what happened here. Maybe this was just an issue moving it over to the new robloxdev site? But this should be addressed. Tables are confusing to learn, and the tutorials should be as clear as possible.
I don’t have the benchmarks in front of me but I can easily bring them up.
This test only shows the speed difference between table.insert and using indexes. table.remove is so slow I couldn’t get it to not timeout.
local LENGTH = 2e7
local list1 = {}
local t0 = tick()
for _=1,LENGTH do
table.insert(list1, 1)
end
print("table.insert speed:", tick() - t0)
local list2 = {}
t0 = tick()
for _=1,LENGTH do
list2[#list2 + 1] = 1
end
print("list[#list + 1] speed:", tick() - t0)
Yeah table manipulation functions are slower, but that’s nothing new. Typically table.insert is fine to use unless you’re having weird performance issues (e.g. trying to insert a million indices at startup, or something like that)
table.remove is slow because it has to shift everything. It’s very useful if you care about the order of your data. If you don’t care about the order, you can do a “fast remove” which is O(1):
function FastRemove(t, i)
local n = #t
t[i] = t[n]
t[n] = nil
end
But that’s kinda besides the point. The table tutorial article is really strange right now, and I’m hoping it can get cleaned up a bit for the sake of new users trying to learn.