For Loop VS TweenService (Pros & Cons)

Alright, so I just want to hear some of the Pros and Cons of using a ForLoop for tweening Part Transparency VS using TweenService to tween Part Transparency:

Benchmarks:

local Time = os.clock()
local TS = game:GetService("TweenService")
local Tween_Info = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)

local Part = game.Workspace.Part2

TS:Create(Part, Tween_Info, {Transparency=1}):Play()

print("Tween Service finished in:", os.clock() - Time)

Output:

Tween Service finished in: 0.00004762501339428127

For Loop:

local Time = os.clock()

local Part = game.Workspace.Part1

for i=0,1,0.1 do
	Part.Transparency = i
end

print("For Loop finished in:", os.clock() - Time)
For Loop finished in: 0.000026915979105979204

As You See, For Loops are faster by just around 0.00002 seconds.

The difference is so small that it doesn’t really matter. I would still use TweenService since it’s more flexible since you can set how long you want it to take, the easing style, and direction. Not only that you can also tween multiple properties like Transparency and Size.

True, but when It comes to only one property, for loops seem more efficient.

Transparency is easy because it’s 0 to 1, but if you were to use a for loop on say a CFrame value things would get a lot more complicated, making tweens easier

4 Likes

The other difference is that tweens do not disrupt the current thread of execution, they are handled by the task schedular similarly to Debris:AddItem.

1 Like

True, so would spawning a new thread and then performing the for loop be slower or still faster.

In the end this would be a useless optimization since Tweens are arguably going to be more optimized when it comes to writing properties to the target of your tween. Your for loop may have been faster, but it only did 10 loops, whereas TweenService may have done 20, or 40 for all we know.

This is a fairly useless debate, TweenService should be used so that you can go on about your day.
The only time you ought to need to use a loop to tween something is when you have a specific effect in mind or a specific property/variable that TweenService doesn’t support.

2 Likes

Yeah, this topic wasn’t needed for a specific reason, I was just curious.