jQuery .closest( 'tag' ) vs custom closestByTagName( 'tag' ) (v2)

Revision 2 of this benchmark created by Eduardo Sada on


Preparation HTML

<section>
<div><div><div><div><aside id="end" class="end"><div><div><div><div><div><div><div><div>
<article>
<p>
<span id="start" class="start"></span>
</p>
</article>
</div></div></div></div></div></div></div></div></aside></div></div></div></div>
</section>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

Setup

var startJQ = $( '#start' ),
        startDOM = $( '#start' )[0];
    
    
    Element.prototype.closestByTagName = function ( tagName ) {
    
        return this.tagName && this.tagName.toUpperCase() == tagName.toUpperCase()
            ? this
            : this.parentNode.closestByTagName && this.parentNode.closestByTagName( tagName );
    
    };

Test runner

Ready to run.

Testing in
TestOps/sec
.closest( 'tag' )
var element = startJQ.closest( 'aspan' )
ready
custom closestByTagName( 'tag' )
var element = startDOM.closestByTagName( 'aspan' )
ready

Revisions

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

  • Revision 1: published by ThinkingStiff on
  • Revision 2: published by Eduardo Sada on