How expensive are GetChildren and GetDescendants on performance?

When you iterate through all of the children of an object, how much does it affect performance?

for i,Object in pairs(Character:GetChildren()) do
      -- Run this code on each object
end

I believe GetDescendants() is more heavy on performance by default, so the same question applies to it as well (since it is quite similar to GetChildren()).

My script actually has an infinite loop that uses GetChildren() every second so if this does cause performance issues then there would be a reason to change the script.

3 Likes

GetDescendants() is more taxing than GetChildren().
This is what the Developer Page says:

The GetDescendants function of an object returns an array that contains all of the descendants of that object. Unlike Instance:GetChildren , which only returns the immediate children of an object, GetDescendants will find every child of the object, every child of those children, and so on and so forth.

1 Like

It depends on how many children your loop is iterating through, and the complexity of whatever you’re doing in each iteration.

1 Like

Well, if you are only going to loop through a few parts with objects in it, it wouldn’t really effect performance either way, however if you are looping through 1000+ objects inside workspace then GetDescendants() would be more heavy on performance, because it is getting every single thing inside workspace, or whatever you are looping through.

3 Likes