Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery first level children selector best way

What is the best (most productive, less CPU-loading) way to select certain children from ancestor with jQuery?

Let's imagine that we have:

<div>
 <b>1</b>
 <p>2</p>
 <a>3</a>
 <p>2</p>
</div>

So

$('div > p')

or

$('div').children('p')

?

like image 567
s.webbandit Avatar asked May 15 '12 08:05

s.webbandit


1 Answers

According to this jsPerf test I just created $('div > p') is about twice as fast. I don't think the two selectors differ in the elements they get back, so the first one may be more desirable, at least from a performance point of view.

like image 174
Ben Everard Avatar answered Oct 13 '22 05:10

Ben Everard