Does jQuery traverse the DOM everytime we use $() to access the same element?

16 Views Asked by At

After clicking the button, does jQuery traverse the DOM from the root again or is the child element cached? Will doing something like var childElement = $('.child') always make it possible to access the child element directly? Is saving a jQuery object in a variable practiced by developers?

$('button').click(function() {
  $('.child').append('Text. ');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button>Add text</button>

<div class="parent5">
  <div class="parent4">
    <div class="parent3">
      <div class="parent2">
        <div class="parent1">
          <div class="child">
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

0

There are 0 best solutions below