jQuery .find() vs. context selector vs non-context selector (v138)

Revision 138 of this benchmark created on


Description

comparing selector speed

Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<p><ins></ins></p>

<article>
        <p>A test</p>
        <p><ins></ins></p>
        <p>This is text</p>
</article>

<p><ins></ins></p>

<article>
        <p>A test</p>
        <p><ins></ins></p>
        <p>This is text</p>
</article>
 

<p><ins></ins></p>

<article>
        <p>A test</p>
        <p><ins></ins></p>
        <p>This is text</p>
        <article>
                <p>A test</p>
                <p><ins></ins></p>
                <p>This is text</p>
                <article>
                        <p>A test</p>
                        <p><ins></ins></p>
                        <p>This is text</p>
                        <article>
                                <p>A test</p>
                                <p><ins></ins></p>
                                <p>This is text</p>
                                <article>
                                        <p>A test</p>
                                        <p><ins></ins></p>
                                        <p>This is text</p>
                                        <article>
                                                <p>A test</p>
                                                <p><ins></ins></p>
                                                <p>This is text</p>
                                                        <article>
                                                                <p>A test</p>
                                                                <p><ins></ins></p>
                                                                <p>This is text</p>
                                                        </article>
                                        </article>
                                </article>
                        </article>
                </article>
        </article>
</article>
                
<article>
        <p>A test</p>
        <div>
                <div>
                        <div>
                                <p><ins></ins></p>
                        </div>
                </div>
        </div>
        <p>This is text</p>
</article>

Setup

var $article = $('article');
    var article = $article[0];
    var articles = $article.get();
    var $$ = document.querySelectorAll;

Test runner

Ready to run.

Testing in
TestOps/sec
find method (node context)
var has = $(articles).find('ins').length > 0 ? true : false;
ready
find method (jquery context)
var has = $articles.find('ins').length > 0 ? true : false;
ready
context node
var has = false;
articles.forEach(function (node, i) {
  // test find first, otherwise check is skipped after first positive check.
  has = !!$(node).find('ins').length || has; 
});
ready
context jQuery
var $ins = $();
$('article').get().forEach(function(node, i) {
  $.merge($ins, $(node).find('ins'));
});
var has = !!$.unique($ins).length;
ready
context jQuery[0]
var has = $('ins', $article[0]).length > 0 ? true : false;
ready
cascade
var has = $('article ins').length > 0 ? true : false;
ready
context (no cached)
var has = $('ins', 'article').length > 0 ? true : false;
ready
select and find
var has = $('article').find('ins').length > 0 ? true : false;
ready
'parent > child' selector
var has = $('article > ins').length > 0 ? true : false;
ready
el qsa
var has = article.querySelectorAll('ins').length > 0 ? true : false;
ready
el qsa direct
var has = article.querySelectorAll('> ins').length > 0 ? true : false;
 
ready
el tagname
var has = article.getElementsByTagName('ins').length > 0 ? true : false;
ready
doc qsa
var has = document.querySelectorAll('article ins').length > 0 ? true : false;
 
ready
soc qsa direct
var has = document.querySelectorAll('article > ins').length > 0 ? true : false;
ready
zest direct
var has = zest('article > ins').length > 0 ? true : false;
ready
zest
var has = zest('article ins').length > 0 ? true : false;
ready
zest cached
var has = zest(article).find('ins').length > 0 ? true : false;
ready
zest context
var has = zest('ins', article).length > 0 ? true : false;
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.