Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Finding parent element: Closest vs Loop vs Recursion
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Browser:
Chrome 140
Operating system:
Windows
Device Platform:
Desktop
Date tested:
8 months ago
Test name
Executions per second
Closest
8211660.5 Ops/sec
Loop
8655030.0 Ops/sec
Recursion
8792291.0 Ops/sec
HTML Preparation code:
<div class="parent"> <div class="child"> <div class="child"> <div class="child child-last"></div> </div> </div> </div>
Script Preparation code:
Benchmark.prototype.setup = function() { var isParent = function(el) { return el.classList.contains('parent'); } var findParent = function(el) { if (isParent(el)) return el if (!el.parentNode) return false return findParent(el.parentNode) } var element = document.getElementsByClassName('child-last')[0]; };
Tests:
Closest
element = element.closest('.parent')
Loop
while (element) { if (isParent(element)) break; if (!element.parentNode) break; element = element.parentNode; }
Recursion
element = findParent(element)