Reputation: 5494
why are some developers using JQuery's .find-function like this:
$(document).find('.progress').show();
If you know the name of the class, you can just use:
$('.progress').show();
Even if i have ul-lists and want to select all li-children i can do this just with
$('li')...
Appreciate some clarity...Thanks!
Upvotes: 0
Views: 195
Reputation: 13727
There is a performance hit to using a class directly since it doesn't map to native JS like getElementbyID
or getElementbyTagName
find()
will often be much more efficient (for pages with a large number of elements).
Edit::
Found the article I remembered this from. #5 here http://dumitruglavan.com/jquery-performance-tips-cheat-sheet/
- Use find() rather than context Indeed, the .find() function seems to be faster. But this counts more when you have a lot of traversing a page with lots of DOM elements:
var divs = $('.testdiv', '#pageBody'); // 2353 on Firebug 3.6
var divs = $('#pageBody').find('.testdiv'); // 2324 on Firebug 3.6 - The best time
var divs = $('#pageBody .testdiv'); // 2469 on Firebug 3.6
Upvotes: 1
Reputation: 887767
The point of .find()
is to find elements within a parent element.
There is no point in $(document).find(...)
.
Upvotes: 2