To represent records, you use the field name as an index. Lua supports this representation by providing a.name as syntactic sugar for a[ânameâ]. So, we could write the last lines of the previous example in a cleanlier manner as
a.x = 10 -- same as a["x"] = 10
print(a.x) -- same as print(a["x"])
print(a.y) -- same as print(a["y
If you want a performance test as your title mentioned, I ran this code (not sure if I did it right though)
local start = os.clock()
for _ = 1, 1000000 do
local part = workspace["Baseplate"]
end
print("Square brackets: " .. os.clock() - start)
local start2 = os.clock()
for _ = 1, 1000000 do
local part = workspace.Baseplate
end
print("Dot: " .. os.clock() - start2)
And got these results
Smaller values also yielded near similar results (most of the time showing that the dot is slightly faster), so thereâs no different between them, itâs personal preference, but square brackets allow you to get items with spaces in their names or getting items with a variable/string value and numbers, which you canât do with the dot I believe, so theyâre more versatile than the dot
Theyâre different ways of doing the same thing as @Syntheix mentioned, so even without a performance test it wouldnât be too difficult to determine that there wouldnât be any performance slowdown that would seriously impact a game
Theyâre usually identical.
If youâre working with local variables, itâs all the same. But if youâre working with global variables (like âgameâ, âworkspaceâ, âscriptâ, etc) then Robloxâs Luau optimizations will save you a few instructions:
local a = game.Workspace.Model.Part.Folder.Value.Value
--
GETIMPORT R4 3 [game.Workspace.Model] -- the important line
GETTABLEKS R3 R4 K4 ['Part']
GETTABLEKS R2 R3 K5 ['Folder']
GETTABLEKS R1 R2 K6 ['Value']
GETTABLEKS R0 R1 K6 ['Value']