Should I use select() or a table?

Which is better and why?
Option 1:

local Items = {GetItem(0), GetItem(1)}
Activated:Connect(function()
    DoStuffWith(Items[math.random(1, 2)])
end)

Option 2:

local Item0 = GetItem(0)
local Item1 = GetItem(1)
Activated:Connect(function()
    DoStuffWith(select(math.random(1, 2), Item0, Item1))
end)

I’d personally use a table in case I have to reference the object later and want to do it easily (plus it works with modules), plus I don’t have to worry about manually adding the argument to the select function, but it’s up to you.

If you’re looking for it from a performance standpoint though, indexing seems to generally be a bit faster than select from my tests:

local obj1 = 1 
local obj2 = 2 
local t = tick() 
for i = 1,1000 do 
    local selected = select(math.random(1,2), obj1, obj2) 
end 
local ft = tick() - t 
print('Finished 1000 selects in',string.format('%f', ft)) 
wait(3) -- just to be sure no memory is being used up for whatever reason
local obj = {1;2} 
local t = tick() 
for i = 1,1000 do 
    local selected = obj[math.random(1,#obj)] 
end 
local ft = tick() - t 
print('Finished 1000 indexes in',string.format('%f', ft))

Test 1:


2:

3:

4:

5:

1 Like