Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery selectors speed

Which of these two is better/faster?

var a = $('.listItem', $('#myList'));

vs

var a = $('#myList .listItem');
like image 367
Eeyore Avatar asked Mar 01 '23 04:03

Eeyore


2 Answers

First of all, you're doing it wrong in a weird way. It should be:

var a = $('.listItem', '#myList');

And according to Resig, the find() method has proven to be quickest in most cases:

var a = $("#myList").find(".listItem");
like image 136
peirix Avatar answered Mar 10 '23 23:03

peirix


The only real way to know is to profile them, that is really the only answer that can be given to the question. There will be a slight performance hit with the first since context works best when it is an element and not a jQuery object.

like image 44
Russ Cam Avatar answered Mar 11 '23 01:03

Russ Cam