Element’s presence in ancestor

Benchmark created on


Description

closest versus matches for testing whether an element has a particular ancestor

Preparation HTML

<u class="ancestor"><u class="target"></u></u>

<u class="ancestor">
	<u>
		<u>
			<u>
				<u>
					<u>
						<u>
							<u>
								<u>
									<u class="target"></u></u>
								</u>
							</u>
						</u>
					</u>
				</u>
			</u>
		</u>
	</u>
</u>

<!-- Without ancestor -->
<u class="target"/>

Setup

const targetList = document.querySelectorAll('.target')

Test runner

Ready to run.

Testing in
TestOps/sec
Closest
for (const target of targetList) {
	null !== target.closest('.ancestor')
}
ready
Matches
for (const target of targetList) {
	target.matches('.ancestor :scope')
}
ready

Revisions

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