Reputation: 6916
Let's say I have huge tree view and one the branches is in variable called $branch
and now I want to check does that branch contain none or more than one element with expand classes. In other words boolean should be returned as soon as one match is found.
Okay, I can do this with $branch.find('.expand').length > 0
, but is there a better way that would stop on the first matching element and therefore would be faster?
I think using first()
reduces existing set to length of one, so I would have use find()
and then reduce?
Ps. As you noticed in the example I used find()
because I want to go deeper than just children of the $branch
Upvotes: 1
Views: 388
Reputation: 14737
You definitely want to take a look at the .has()
method.
var $branchesWithExpand = $branch.has('.expand');
Upvotes: 1
Reputation: 21713
Standard CSS selectors like this use the native DOM method querySelectorAll(). I.e. they run in the browser's native code and they're fast. Unless you've got reason to think that doing the above is slow, I doubt you need to worry about it.
Upvotes: 1