jQuery .closest( 'tag' ) vs DOM Element.closestByTagName( 'tag' )

JavaScript performance comparison

Test case created by ThinkingStiff

Preparation code

<section>
<div><div><div><div><div id="end"><div><div><div><div><div><div><div><div>
<article>
<p>
<span id="start"></span>
</p>
</article>
</div></div></div></div></div></div></div></div></div></div></div></div></div>
</section>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    var startJQ = $( '#start' ),
        startDOM = $( '#start' )[0];
   
    Element.prototype.closestByTagName = function( tagName )
    {
       
        if( this.parentNode.nodeType == Node.ELEMENT_NODE )
        {        
           
            return this.tagName == tagName.toUpperCase()
                ? this
                : this.parentNode.closestByTagName( tagName );
           
        };
       
    };
   
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
.closest( 'tag' )
var jq1 = startJQ.closest( 'section' ),
    jq2 = startJQ.closest( 'pre' ); //test not found

 
pending…
.closestByTagName('tag')
var dom1 = startDOM.closestByTagName( 'section' ),
    dom2 = startDOM.closestByTagName( 'pre' ); //test not found
pending…

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

Compare results of other browsers

0 comments

Add a comment