Is looping the same function several times (in a for loop) a bad thing?

I’m making a game and it involves a Folder holding around 60 attributes (instead of Values, as in StringValues, NumberValues, etc). I’m not planning on changing this method, but I thought it would be important to mention it.

The problem : I have to loop through all the 60 attributes and set them each to 0, so I use a for loop. Normally, I would just do this:

for i,_ in pairs(Folder:GetAttributes()) do
	--// Code...
	Folder:SetAttribute(i, 0)
end

However, I want to be able to re-use that --// Code... part by instead putting it in a function and using it. It will thus turn into:

--// The reason I put it in a function is to be able to use it in other scripts via a module
function Module:Something(Attribute)
	--// Code...
	Folder:SetAttribute(Attribute, 0)
end

--// Around 60 attributes
for i,_ in pairs(Folder:GetAttributes()) do
	Something(i)
	--// No task.wait(), no delay
end

Is this bad, running the same function 60 times in a row without any delay? I’m wondering if it’ll put any stress/lag on the game/memory, or if it’ll be the same as just directly using --// Code... in the for loop itself.

I’ve tried searching up this same question but I couldn’t find anything. Apologies if I didn’t search well.

It depends on the complexity of the function, but in your case, this code would run near instantly, even if you had thousands of attributes. You really only need to worry about delays in loops where the execution time is uncertain, or the loop is indefinite.

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