Is :FindFirstChild worse than ["part"]?

As stated before, : FindFirstChild is more slower than the dot by almost 8 times, but what about [“part”]?, Is it better than FindFirstChild? Is it the same speed as the dot operator?

I’m asking this bc I find nothing in the search result.

Doing ["part"] and :FindFirstChild("part") are fundamentally different things.

If you attempt to index an Instance with a key and that key does not associate as a property or a child of the instance then an error will be thrown. The purpose of :FindFirstChild is to access the instance for a child of a given name, but not throw an error if it does not exist.

There is no “speed benefit” to using dot syntax or bracket syntax because they compile the same. The only difference is that it would required to use bracket syntax when you’re indexing for an instance with a non-alphabetic character such as space or $ in the name, OR if you are wishing to index the instance with the value of a variable, local a = "Part"; b[a]

2 Likes

Uhh but when I do FindFirstChild on an non existent object it throws an error

You didn’t understand my post. Obviously you cannot call :FindFirstChild on something that is not an instance in the first place because it won’t have such a method.

Ex.

local a = Instance.new("Part")
local hello = a.hello

error immediately thrown because this new instance a will clearly have no “hello” child

local a = Instance.new("Part")
local hello = a:FindFirstChild("hello")

No error is thrown because :FindFirstChild was used which will return nil` instead of throwing an error.

I am not an idiot, I was creating a rock system and when I did (to check if it’s mineable) if FindFirstChild it returned an error

Oh, I get it, bc it was non existent, so “if nil then” lol.

Couldve done “if FindFirstChild ~= nil then”

Yes this is the point of using :FindFirstChild. Instead of throwing an error and stopping the code entirely, you can handle for the instance not existing.

1 Like