Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
classList.contains vs Custom Loop vs .matches() v2
Comparing performance of checking multiple classes using native chaining, a custom prototype function with a `for` loop, and the `matches()` selector method.
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0
Browser:
Chrome 144
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
3 months ago
Test name
Executions per second
Chained Native `contains`
6882659.0 Ops/sec
Custom .classCheck()
6516033.5 Ops/sec
Native .matches()
8243504.0 Ops/sec
HTML Preparation code:
<div id="container"> <div class="btn primary loading"></div> <div class="btn secondary active"></div> <div class="btn primary hidden"></div> <div class="btn large primary loading"></div> </div>
Script Preparation code:
Element.prototype.classCheck = function(classArray) { for (let i = 0; i < classArray.length; i++) { if (!this.classList.contains(classArray[i])) return false; } return true; }; const elements = Array.from(document.querySelectorAll('#container div')); const classesToTest = ['primary', 'loading']; const selectorToTest = '.primary.loading'; let counter = 0;
Tests:
Chained Native `contains`
const el = elements[counter++ % elements.length]; return el.classList.contains('primary') && el.classList.contains('loading');
Custom .classCheck()
const el = elements[counter++ % elements.length]; return el.classCheck(classesToTest);
Native .matches()
const el = elements[counter++ % elements.length]; return el.matches(selectorToTest);