Is Parent["Child"] the same as Parent.Child performance wise?

Hello devforum, I am wondering if doing something like this:

local rs = game.ReplicatedStorage
local remotes = rs["Remotes"]

would affect performance or would it be the same (performance) as using:

local rs = game.ReplicatedStorage
local remotes = rs.Remotes
1 Like

Yes, there are both the same. One is just syntactic sugar for the other.

2.5 Tables

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
1 Like

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

image

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']
local b = game["Workspace"]["Model"]["Part"]["Folder"]["Value"]["Value"]
--
GETIMPORT R7 7 [game]
GETTABLEKS R6 R7 K1 ['Workspace']
GETTABLEKS R5 R6 K2 ['Model']
GETTABLEKS R4 R5 K4 ['Part']
GETTABLEKS R3 R4 K5 ['Folder']
GETTABLEKS R2 R3 K6 ['Value']
GETTABLEKS R1 R2 K6 ['Value']

That’s still not enough to make a difference, and expect Luau to optimize this code further when you put it into a context.

1 Like

You all helped thanks, couldn’t mark them all as solutions though lol

2 Likes

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