Alex Rivera | Logout

Which is more efficient: .parent().parent().parent() ~or~ parents(".foo") ~or~ closest(".foo")

Asked 2010-02-15T09:35:18.060
34

I have an A tag which triggers the animation of it's great-great-great-grandparent. All of the following will work, but which is most efficient, and why?

$(this).parent().parent().parent().parent().parent().animate(...);

$(this).parents(".foo").animate(...);

$(this).closest(".foo").animate(...);

I suspect that the first might be, as it's the most explicit, but for maintenance reasons (the nesting may change) I prefer the second. They all appear to run smoothly in practice.

Edit
Report

1 Answer

6

It is a very good thing that you did performance measurements. That's exactly what should be done in such scenarios. If all appear to run smoothly in practice and you are satisfied with the performance pick the most readable one (second and third look ok).

answered 2010-02-15T09:38:31.523

Your Answer