Which of these two is better/faster?
var a = $('.listItem', $('#myList'));
vs
var a = $('#myList .listItem');
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");
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With