Element.closest vs parent walk (v3)

Revision 3 of this benchmark created by Dima Voytenko on


Preparation HTML

<a>
  <div class="outer">
    <div class="inner" id="inner">
      <img src="" id="img1">
    </div>
  </div>
</a>

Setup

var inner = document.getElementById('inner');
    
    function closest(element, tagName) {
      tagName = tagName.toUpperCase();
      var el;
      for (el = element; el; el = el.parentElement) {
        if (el.tagName == tagName) {
          return el;
        }
      }
      return null;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Element.closest
var img1 = document.createElement('img');
inner.appendChild(img1);
img1.closest('a');
inner.removeChild(img1);
 
ready
parent walk
var img1 = document.createElement('img');
inner.appendChild(img1);
closest(img1, 'a')
inner.removeChild(img1);
 
ready

Revisions

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

  • Revision 3: published by Dima Voytenko on