Which is a better way to remove item from list

Is one better than the other?

Players.PlayerRemoving:Connect(function(player)
	for i = 1, #queue do
		if player == queue[i] then
			table.remove(queue, i)
		end
	end
end)
Players.PlayerRemoving:Connect(function(player)
	table.remove(queue, table.find(queue, player))
end)

This is a benchmark I ran and this is the result:
image
table.find is faster.

Script in question?

local list = {}

local char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

for i = 1,1000 do
	local str = ""
	for x = 1,5 do
		local hash = math.random(1,#char)
		str ..= char:sub(hash,hash)
	end
	table.insert(list,str)
end

local randomChar = list[math.random(1,1000)]

local st = tick()

for i = 1, #list do
	if randomChar == list[i] then
		table.remove(list, i)
	end
end

print("Took "..tick()-st.." seconds.")

st = tick()

table.remove(list,table.find(list,randomChar))

print("Took "..tick()-st.." seconds.")
2 Likes

I should have added a break after removing the player so that test shows worst case scenario but table.find would still be better.

Yeah but it’s still a stupid idea to do it like that… table.find literally exist why do you want to replace it?

Anyways glad I could help!!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.