Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

ipairs or table[index]: Which is more efficient?

I have two functions. Which is better and why?

tabIndexOverflow = function(seed, table)
	for i = 1, #table do
		if seed - table[i] <= 0 then
			return i, seed
		end
		seed = seed - table[i]
	end
end

tabIndexOverflow = function(seed, table)
	for i, value in ipairs(table) do
		if seed - value <= 0 then
			return i, seed
		end
		seed = seed - value
	end
end

1 answer

✓ Accepted answer

Good question.

ipairs() is about 200% slower than a numerical loop

tabIndexOverflow = function(seed, table)
    for i = 1, #table do
	local v = table[i];
        if seed - v <= 0 then
            return i, seed
        end
        seed = seed - v
    end
end

That'd be your best bet for speed, without any further information of what your function is attempting to accomplish.

2 comments

0

Source? Where does 200% come from?

This discussion is preserved as it was. New answers, comments, and votes are closed.