Do if statements use up more lag in RenderStepped events?

For context, I have a RenderStepped loop in a LocalScript (Mainly used for camera manipulation), and I have a little if statement that changes a variable (the currentWay) based on a number calculated from the mouse’s and the camera’s positions.

I was wondering which option reduces more lag

This:

if yRot <= -(50 / 100) then
		currentWay = "Right"
end

or this?:

if yRot <= -(50 / 100) then
		if currentWay ~= "Right" then
			currentWay = "Right"
		end
	end

Looking forward to hearing from you!

(Also should this post be in Development Discussion instead?)

1 Like

I don’t think this belongs in Development Discussion, here is fine

Anyway, while the lag amount is miniscule, the second way would cause slightly more lag as you are doing a more advanced operation. Either way you’re checking something every frame, but with the first way you’re only setting a variable instead of doing an if operation then setting a variable if condition = true.

IMO the best way to do this would just have a loop grab on to whenever the yRot variable changes and do your code there :^)

3 Likes

I’d recommend the first one because it has less instructions:

3 instructions

if yRot <= -(50 / 100) then -- Reads yRot, checks if condition is true
		currentWay = "Right" -- Writes "Right" to currentWay
end

4 if false, 5 if true

if yRot <= -(50 / 100) then -- Reads yRot, checks if condition is true
		if currentWay ~= "Right" then -- Reads currentWay, checks if it's equal to "Right"
			currentWay = "Right" -- Writes "Right" to currentWay
		end
	end

But I think you shouldn’t make a variable, and instead just calculate it each time you need to use it.

And as @RealMysticall already stated, the difference in lag is minuscule. But I still think you should use the first one because of readability.

4 Likes

We need to use something to keep track of things, right? :slight_smile:

Anyhow, thanks for replying and answering my question!

2 Likes

We need to use something to keep track of things, right? :slight_smile:

I mean you could make a function like getCurrentWay() that would calculate it instead of manually writing the math each time, but I mean it’s 5 chars added so use at your own will!

1 Like

Wouldn’t change too much, to be honest.
I mean, we still have to call the function somehow, which in the end makes it (IMO) quite unnecessary to use.
I’d only create get[any]() functions in module scripts (when something in it needs to be retrieved). I’m not experienced in these lag reducing things, so let me know if I’m wrong about something!

2 Likes

What you’re asking about is really only relevant for changing properties of Instances, since Roblox sandboxes everything and there is a significant overhead with property/method calls if they’re being accessed extremely frequently (thousands of times per frame)

If you don’t know what I’m talking about, here’s an example:

--outside loop
local prevVisibilityState: boolean

--inside loop
if prevVisibilityState ~= newVisibilityState then
	prevVisibilityState = newVisibilityState
	GuiObject.Visible = newVisibilityState --this will only run if the visibility state changes
end

By doing something like this, you avoid ever touching the Roblox API unless it’s absolutely necessary. All the logic stays confined within the script itself, which is well optimized.

Constantly changing the GuiObject’s .Visible property can incur an overhead (even if the value itself didn’t change) because writing anything to anything inside Roblox’s API will first pass through metatable checks and other internal engine stuff.

2 Likes

I made a reply to a question like this pretty recently if you want to check out some benchmarks:

1 Like

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