views:

282

answers:

4

Which of these two is better/faster?

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

vs

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

The second is definitely easier to read and would make the code easier to maintain.

...that in my opinion makes it better.

They should both produce similar performance results.

Justin Niessner
In my case speed is more important than readability.
Eeyore
+3  A: 

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.

Russ Cam
+5  A: 

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");
peirix
the context should ideally be an element to fully take advantage of it.
Russ Cam
Please note that the difference in speed between the two of these is so negligible that unless you're doing this 1000 times on a page, no one is likely to notice.
Bob Aman
@Bob, yeah, that's very true. As someone stated before on SO (can't remember where): If you're looking to speed up your website, start somewhere else than jQuery...I'd say that's a fairly good advice in most cases..
peirix
Your two examples will yield **completely** different results. And he is *not* doing it wrong.
KyleFarris
@Kyle: How will they yield different results? They're both looking inside `#myList` for elements with a class name of `listItem`. But you're right, he wasn't doing it wrong, he was simply doing using bad practice.
peirix
A: 

For the record,

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

will perform exactly the same as:

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

In my tests, this works the fastest:

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

Oh, and:

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

is completely wrong. (edit) is the same as the first example.


EDIT:

I'm a moron. The last example is exactly the same as the first example in terms of functionality. As for speed, I can't say. My wild guess would be that since, in the first example, the jQuery object already has the elements asked for by the selector, it would be slightly faster than the last example that would still have to find the elements.

KyleFarris
We all hear you on the **completely** wrong part. But would you mind explaining it? Just saying it's wrong isn't helping anyone.
peirix
But `#myList > .listItem` is not the same as `#myList .listItem`.
Gumbo
I am not so sure this is true as someone pointed out on freenode that var a = $('.listItem',$('#myList')); actually makes an extra function call.
cballou
@peirix: The completely wrong version is doing an OR operation. It finds all elements that either have the class `.listItem` or the id `#myList`. Also, just to clarify why `$('#myList > .listItem')` is faster -- because it is only looking for `.listItem` elements that are immediate children of `#myList`. In contrast, `$('#myList .listItem')` finds any `.listItem` element below `#myList`, so there is potentially more DOM to search through.
Tauren
@Tauren. Umm...No, `$(".listItem", "#myList")` looks for `.listItem` inside `#myList`... To look for either, the comma needs to be inside the quotation marks like this: `$("listItem, #myList")`, but that's not an `OR`, that's an `AND`.
peirix
@peirix: of course, you are correct about `$(".listItem", "#myList")`. Serves me right for attempting to comment at 3AM. For whatever reason, I wasn't seeing the quotes right, and thought we were talking about `$(".listItem, #myList")`.
Tauren
@peirix: However, I'm not convinced the comma creates a logic AND operation -- I believe it is an OR operation. This is because an AND operation would mean that only elements that had BOTH a class of `.listItem` AND an id of `#myList` would be selected. But using the comma selects all elements of any of the specified selectors, so elements with class `.listItem` OR elements with id `#myList` would be selected. If there was no `#myList` in the document, any `.listItem` elements would still be selected.
Tauren
@Tauren I then think we agree, but see it a bit different :p I would say it's an `AND` because it would select all `.listItem` **and** all `#myList`(hopefully only one element). But I guess we're both right/wrong, as it would select any, both or none, depending on what is in the document (:
peirix
Admittedly, I was being stupid when I said the last example is completely wrong. In fact, I was completely wrong. I said something wouldn't work without even testing it first. I assumed you had to pass a jQuery object (ex. $('test')) and not just aselector (ex. 'test') as the second parameter.I should be punched in the face for spreading false information. In any case, the version using the ">" is still faster **if** you are specifically trying to select the direct descendants (and nothing else) as the original question **seemed** to be wanting to do. Of course, I could be wrong again.
KyleFarris