Which is faster pcall with dot operator or :FindFirstChild()

I need this because Im making a module and Im making it so if some parts are not inserted that feature will not be provided for that part, for example in my voting module if someone doesn’t insert an image part in the voting pad then nothing would error but if they did there would be an image decal on it. So I need your help which is faster pcall() with dot operator or :FindFirstChild()

1 Like
  1. I think FindFirstChild is probably faster, but I haven’t tested it

  2. Speed shouldn’t be the reason you pick one over the other. FindFirstChild is what you should use for this situation because even if pcall is faster, you aren’t calling it like 100 times a second when speed would actually be needed.

  3. FindFirstChild has more purposes than what . can provide:

item:FindFirstChild(name) returns false if it doesn’t exist, and the item if it does exist. Also, since name is a string, you can search for names like "t e s t", which you can’t do with ..

So if you still aren’t going to use FindFirstChild, i’d recommend using item[name] instead of item.name.

item[“name”] errors to if the item does not exist. So you reccomond :FindFirstChild() over call right? And yes I need a lot of speed in this, so you reccomond :FindFirstChild()?

Yes. Also the difference is going to be so small that the speed difference won’t even matter, even if you need a lot of speed.

Oh alright, I thought it would because in the roblox article they said :FindFirstChild() is 30% slower

You shouldn’t let the statistics mislead you, because both are pretty much instantaneous. Also, would you mind linking that article?

Oops I meant 20% and yes here

image

local t = tick()
local module = script:FindFirstChild("ModuleScript")
print(tick() - t)

local g = tick()
local module = script.ModuleScript
print(tick() - g)

they literally took the same exact time, lol
i’ll test with multiple iterations and then update the time in this post.

1 Like

LOL thanks lol, can you maybe PM me a way to test code speed so I can do it myself do? Not just this like other code tests to

In general, throwing an error and catching it tends to be an expensive operation.
In this instance, it seems to be about 10x faster to call FindFirstChild() than using pcall() for a very large number of calls. Although, as has been pointed out, it won’t be noticeable unless you are doing it repeatedly hundreds of thousands of times.

1 Like

ok so last time i forgot to include pcall so I did that this time.
also, this time it’s a 10,000 iteration test.
image

using FindFirstChild was about 1.3x faster than pcall with dot notation.
@CoolShank8, here’s the code if you want to learn how to test other things yourself

local t = tick()
for x = 1, 1e4 do
	local module = script:FindFirstChild("ModuleScript")
end
local _t = tick() - t

local g = tick()
for x = 1, 1e4 do
	pcall(function()
		local module = script.ModuleScript
	end)
end
local _g = tick() - g

print(_g / _t)

Alright thanks but uh why are you dividing _g and_t

To see how many times faster it is.

1 Like

Speed shouldn’t be a concern here, it’s more about what’s appropriate to be using here. pcall is not appropriate to use here because FindFirstChild is not going to throw an error that is outside of your control unlike what HttpService or DataStores might.

Use FindFirstChild. Don’t add unnecessary obscurity to your code.

Here’s a tutorial someone’s written about using pcall:

3 Likes

I revived this post to add a test result I got based on this thread.

its even faster if you use a variable to test for existence, this will beat both of those methods by a large margin:

so, something like this

ephemeralObject = script:FindFirstChild("ModuleScript")  --keep the dot or findfirstchild out of the loop

--do timed loop here...
if ephemeralObject then
     local module = ephemeralObject --may or may not exist but won't error either way
end
--end loop

if the object doesn’t exist then ‘module’ will be nil and the function doesn’t error and its FAST even with the if statement!

1 Like