What would be more beneficial in this situation? If-Statement Checks or Loops?

Hello! I’ve been pondering this situation for a little bit now but what would be more efficient (performance wise)?

local ListOfValues = {
   Example1,
   Example2
} 

local function OnRenderStepped(dt)
      for i = 1, #ListOfValues do
            if ListOfValues[i].Value then
                  return
            end
      end
end

or

local ListOfValues = {
   Example1,
   Example2
} 

local function OnRenderStepped(dt)

      if ListOfValues.Example1.Value then
            return
      end

      if ListOfValues.Example2.Value then
           return
      end

end

Well considering you are using renderstepped and run service, if it’s a local script doesn’t really matter so go with loops for efficiency but if it’s a server script If values for performance and loops for efficiency.

How do you define efficiency, and how is that different from performance?

Go with loops, there’s not going to be any detectable performance difference.

Hello,

RenderStepped should only be used for camera manipulation and tasks of high priority: RenderStepped, Stepped, or Heartbeat - #2 by rogchamp
I suggest that you use Heartbeat rather than RenderStepped unless it’s necessary.

If you want fast performance, you can eliminate conditional statements by using a function dictionary. However, since you’re utilizing ValueObject instances, you might have to fiddle an alternative with the code below.

local function Example1Function()

end

local function Example2Function()

end

local FunctionDict = {
    ["Example1"] = Example1Function,
    ["Example2"] = Example2Function
}

Otherwise, I believe that the for loop method would be less performant because you’re iterating through a list, as well as checking for conditions.

1 Like

Thank you, exactly what I was lookin’ for

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