Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Chrome 120
Mac OS X 10.15.7
Desktop
11 months ago
Test name Executions per second
while 141006.4 Ops/sec
closest 143635.7 Ops/sec
HTML Preparation code:
x
 
1
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
2
<style type="text/css">
3
.hidden {
4
display: none;
5
}
6
</style>
7
<div class="visible">
8
<div class="hidden">
9
<div class="hidden">
10
<div class="hidden hidden-last"></div>
11
</div>
12
</div>
13
</div>
14
      
15
16
      <script>
17
18
        Benchmark.prototype.setup = function() {
19
20
          
21
  var $element = $('.hidden-last');
22
23
        
24
  };
25
26
27
      
28
</script>
29
    
30
Tests:
  • while

     
    while ($element.is(":hidden")) {
       $element = $element.parent();
    }
  • closest

     
    $element = $element.closest(':visible')