Tuple iterating

I noticed that the first function works faster than the second on luau, but in lua, on the contrary, method 2 is faster. Is it even worth using the first method in roblox?

local function m1(...)
  for i = 1, select('#', ...) do
    local value = select(i, ...)
  end
end

local function m2(...)
  for i, v in pairs{...} do
  end
end
1 Like

Just choose one which is comfortable for you. The difference between two doesn’t affect a lot.

1 Like

for luau, the first method is better in terms of memory allocation and processing speed. however, unless ur iterating through a very heavy table and doing heavy calculations, the difference is basically impossible to notice and useless.

2 Likes

the difference would he unnoticeable, unless there’s lots of calculations involved. You also don’t need to use pairs() anymore

1 Like

Try without the pairs call

Also, would m2 without pairs not be faster than m1 for larger sizes of ...? Could someone benchmark these for both small and large input sizes?

2 Likes

both are O(n), so you can just pick which ever one you like

1 Like

Luau does a pretty damn good job at optimizing method 1!
On O0 (no optimizations), method 1 is many magnitudes slower (to the point where a game using this could be unplayable), but on O1 and above, import, fastcall and other optimizations seem to make it ever so slightly faster than method 2.

O0 (no optimizations):

O2 (‘aggressive’ optimizations):

But as the others have stated, the typical difference in performance is so marginal, you should just use whatever you prefer.

1 Like

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