Tables and performance

hi, im new at tables so idk but people say to many tables is lagy, i have some questions:

what is more lagy,

local Table = {}

or


local Table = table.create('eg')

and how to use table.create()?

and what is more good to use:


for i, v in next, game.Player:GetPlayers() do

or


for i,v in pairs(game.Players:GetPlayers()) do


please i need to know so my Custom DataStore sitem to not be so lagy

table.create does not exist - it’s totally unnecessary because there’s already a way to construct tables.

As for your next question, this thread will help
https://devforum.roblox.com/t/solfed-next-vs-pairs-vs-numeric-for/17369

2 Likes

oh so next is equal to pairs but ipairs are faster?

table.create actually does exist. There is valid use cases for a table.create It is not “unnecessary”

Images

image

As for next vs pairs I suggest using pairs for the fact that it is idiomatically similar to other functions employed in lua such as gmatch

3 Likes

pairs behind the scenes is basically:

local function pairs(tbl)
    return next, tbl, nil
end

There won’t be much difference here.

However, if you’re iterating through arrays then I’d recommend using ipairs as recently in the new VM it has been optimised & it promises ordered iteration.

1 Like

The new compiler employs idiomatic optimizations so that for i, v in pairs is no longer its equivalent function call wrapper, so it’s not exactly the same anymore, but since next has also been added to the list of optimizations they’ll still be about the same speed.

2 Likes

While pairs will work, ipairs is intended for arrays and will likely run a bit faster in those cases. Iteration order is also not necessarily predictable when using pairs/next, while ipairs will always start at 1 and stop at the first nil/hole in the array.

2 Likes