Which one is more optimized?

So I was looking through some docs looking for a way of finding if a thing is parent of a model and I stumbled upon these:

:FindFirstAncestorOfClass()

and

:FindFirstAncestorWhichIsA()

Booth seem to do the relative same and now my question is, which one is more optimized / less performance heavy if run multiple times In quick succession?

1 Like

Of Class is specific
Where IsA is more inheritance based.

Such as IsA will show a part as both “Part” and “BasePart”

but Of Class will only show it as “Part” or something like that.

1 Like

Returns the first child of the [Instance] (Instance | Documentation - Roblox Creator Hub) for whom Instance:IsA() returns true for the given className.
If no matching child is found, this function returns nil. If the optional recursive argument is true, this function searches all descendants rather than only the immediate children of the Instance.
Unlike Instance:FindFirstChildOfClass(), this function uses Instance:IsA() which respects class inheritance. For example:

IsA respects class inheritance aka any subclasses would be recognized. So if you used AncestorOfClass for BasePart, it would only look for BaseParts, and it wouldn’t recognize subclasses like MeshParts from what I understand.

1 Like

Im looking for models so it doesn’t really matter right?

1 Like

Hey you could try using tick() before and after running the function to calculate the time it takes

local Start = tick()
Part:FindFirstAncestorOfClass("Model")
print(tick()-Start)
2 Likes

I mean in this instance, AncestorOfClass would be more specfic, but I don’t particularly know how optimized either of them are. They’re practically almost identical so it shouldn’t matter too much. If you are really concerned you can benchmark it.

2 Likes
  > local average = 0   for i = 1,10000 do local start = tick() workspace.Folder:FindFirstAncestorOfClass("Model") average += tick()-start end print(average/10000)
  0.000001299118995666504
  > local average = 0   for i = 1,10000 do local start = tick() workspace.Folder:FindFirstAncestorWhichIsA("Model") average += tick()-start end print(average/10000)
  0.0000013211488723754882

So not much difference. I’ve seen though that as the test length continues, the average time for the operation goes down.

My structure for the workspace was Folder > Model (A) > Model (B).

There wasn’t really much difference and there are 5 zeroes so it’s a very short operation. I can’t speak to the actual load on the server though.

1 Like

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