Performance Question

Lets say we have an IntValue “A”, and it constantly changes every 0.1 seconds, never faster, never slower. Which one of these codes will be better for performance and why?

A.Changed:Connect(function(newA)
    remoteEvent:FireServer(newA)
end)
local oldA = A.Value
while task.wait(1) do
   if A.Value ~= oldA then
      remoteEvent:FireServer(A.Value)
   end
end

Just use Changed, its much simpler, precise and readable. Number 2 could possibly take up to a whole second to fire the remote event as well as being a loop which means it wont reach any lower lines until it breaks

If you are really concerned about performance I’m not 100% sure but here is what I have loosely heard (don’t quote me)

  • In A you are listening for the IntValue to change, I believe connections simply use a small amount of memory??

  • In B you are having store the old value in a variable
  • Every loop it has to re evaluate if task.wait(1) evaluates to true (if you simply use while true do the code doesn’t bother evaluating btw so just use while true do and add the task.wait inside your loop)
  • Every loop it has to access the value of the int value and then compare if it is not equal to your old variable
1 Like

Both should be equally as performant. This question is better suited in #help-and-feedback:scripting-support

Oh my bad, I thought its in that categoty

1 Like

I also make that mistake many times. There are so many categories haha.

1 Like