jQuery Parents vs Closest vs :first (v39)

Revision 39 of this benchmark created by Jim Montgomery on


Preparation HTML

<div>
  <table id="end" class="end">
    <tr>
      <td>
        <ul>
          <li>
            1
          </li>
          <li>
            2
          </li>
          <li>
            3
          </li>
          <li>
            4
          </li>
          <li>
            5
          </li>
          <li>
            6
            <ul>
              <li>
                1
              </li>
              <li>
                2
              </li>
              <li>
                2
              </li>
              <li>
                3
                <ul class="should-be" id="should-be">
                  <li>
                    1
                  </li>
                  <li>
                    2
                  </li>
                  <li>
                    3
                  </li>
                  <li id="start" class="start">
                    4
                  </li>
                  <li>
                    5
                  </li>
                </ul>
              </li>
              <li>
                4
              </li>
              <li>
                5
              </li>
            </ul>
          </li>
          <li>
            7
          </li>
          <li>
            8
          </li>
          <li>
            9
          </li>
          <li>
            10
          </li>
        </ul>
      </td>
    </tr>
  </table>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>

Setup

var start = $('#start'), shouldBe = $('#should-be'), found;

Teardown


    if(window.console) console.log('found===shouldBe:',found[0]===shouldBe[0]);
  

Test runner

Ready to run.

Testing in
TestOps/sec
parents('.should-be')
found=start.parents('.should-be')
ready
parents('#should-be')
found=start.parents('#should-be')
ready
parents('.should-be:first')
found=start.parents('.should-be:first')
ready
parents('.should-be').first()
found=start.parents('.should-be').first()
ready
parents('ul').first()
found=start.parents('ul').first()
ready
closest('ul')
found=start.closest('ul')
ready
closest('.should-be')
found=start.closest('.should-be')
ready
parents('ul:first')
found=start.parents('ul:first')
ready
closest('#should-be')
found=start.closest('#should-be')
ready

Revisions

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