I am wondering if there is any advantage to using a context parameter when choosing jQuery and using a regular CSS selector.
Assuming I have this html:
<div class="contacts">
<h1>All contacts</h1>
<div class="contact new">
<p class="name">Jim Jones</p>
<p class="phone">(555) 555-1212</p>
</div>
<div class="contact new">
<p class="name">Bob Smith</p>
<p class="phone">(555) 555-1213</p>
</div>
<div class="contact new">
<p class="name">Dave Baker</p>
<p class="phone">(555) 555-1214</p>
</div>
<div class="contact">
<p class="name">Pete Harrison</p>
<p class="phone">(555) 555-1215</p>
</div>
<div class="contact">
<p class="name">George Donald</p>
<p class="phone">(555) 555-1216</p>
</div>
<div class="contact">
<p class="name">Chris Root</p>
<p class="phone">(555) 555-1217</p>
</div>
</div>
If I want to capture all new contacts (marked with a "new" class) from the contacts div, which method is faster, scales better, etc.?
$('.contacts .new');
or
$('.new', '.contacts');
jsFiddle
Update
There are a lot of interesting information in the answers and comments. To summarize the main points, in most browsers, one selector scales better when there are several div.contacts. In most browsers, two selector context methods work faster, only if there is only one .contacts div.
- , , .
$('p:first', '#chapter2');
, .
$('.chapter p:first-child');