What is the fastest loop?

You can make it faster by adding an interations variable:

local maxIterations = 5 -- It will run the loop this many times before waiting
local iterations = 0
while true do
    -- code
    iterations += 1 -- Adding onto the iterations
    if iterations == maxIterations then -- Checking if the threshold is passed
        game:GetService("RunService").Heartbeat:Wait() -- Waiting
        iterations = 0 -- Resetting
    end
end

Although, this may not be beneficial to your use case(s).

Here’s the official documentation. Which is what I was explaining.

Found here: Network Ownership

Any iterations faster than that will possibly crash so why would you want to do that? I guess if you want faster iterations then don’t use wait?

A much easier to code way which I personally use in my setup code in my render engine is

local i = 0
for x=0,width-1 do
  for y=0,height-1 do
    i += 1
    if i %1000 == 0 then
       game:GetService("RunService").Heartbeat:Wait()
    end
  end
end

looping infinitely with a for loop would be:

for i = 1, math.huge() do

end

Running some tests; I have found that a for loop is the fastest.
My proof would be:


image
The code is 30 lines long everything lays in the loop. The while loop is slower. The repeat until loop is equally fast but only on a smaller scale, as things scale up, it may get slower.
Take this with a grain of salt, as all of my proof could be proven invalid at any time.

1 Like

You know it really isnt that hard if you want a faster loop then add a loop inside a loop or a run service inside of a run service think smarter not harder :smirk:

1 Like

try this out

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