Matches: Comma selector vs loop over selectors

Benchmark created on


Preparation HTML

<div id="el"></div>
<div id="el2" class="test"></div>

Setup

window.SELECTORS = [".test"];
for (let i = 0; i < 10000; i++) {
	SELECTORS.push(".x" + crypto.randomUUID());
}
window.BIG_SELECTOR = window.SELECTORS.join(", ");

Test runner

Ready to run.

Testing in
TestOps/sec
Comma-separated selector
el.matches(SELECTORS.join(", "));
ready
Loop
SELECTORS.some(sel => el.matches(sel));
ready
Comma-separated with short circuit
el2.matches(SELECTORS.join(", "));
ready
Loop with short-circuit
SELECTORS.some(sel => el2.matches(sel));
ready
Prepared comma-separated with short circuic
el2.matches(BIG_SELECTOR);
ready

Revisions

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