jQuery .closest( '#id' ) vs DOM Element.closestById( 'id' ) (v3)

Revision 3 of this benchmark created by ThinkingStiff on


Preparation HTML

<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>

Setup

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

Test runner

Ready to run.

Testing in
TestOps/sec
.closest( '#id' )
var jq1 = startJQ.closest( '#end' ),
    jq2 = startJQ.closest( '#not-found' );
 
ready
.closestById( 'id' )
var dom1 = startDOM.closestById( 'end' ),
    dom2 = startDOM.closestById( 'not-found' );
 
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 ThinkingStiff on
  • Revision 3: published by ThinkingStiff on