Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
className vs getAttribute('class') vs classList
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/126.0.0.0 Safari/537.36
Browser:
Chrome 126
Operating system:
Windows
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
get/set Attribute
317.0 Ops/sec
+= className
152.3 Ops/sec
classList.add()
38896.6 Ops/sec
HTML Preparation code:
<div id="root" class="whopper w-screen h-screen bg-neutral-150 flex-row center"> <div class="whiplash pos-rel w-500 min-h-full bg-neutral-250 flex-col overflow-hidden"> <div class="whoop"> <div class="wheep"> <div class="whyne-stein pos-rel mg-x-200 mg-y-auto pd-500 bg-neutral-500 cursor-pointer"> <span class="joker text-bold text-xl text-white bg-black pd-400 rounded-full">WHY SO SERIOUS?</span> </div> </div> </div> </div> </div>
Script Preparation code:
function tagElementsByAttribute(children) { for (let i = 0, child, cls; i < children.length; i++) { child = children[i]; child.setAttribute('class', child.getAttribute('class') + ' tagged'); if (child.firstElementChild) { tagElementsByAttribute(child.children); } } } function tagElementsByClassName(children) { for (let i = 0, child, cls; i < children.length; i++) { child = children[i]; child.className += ' tagged'; if (child.firstElementChild) { tagElementsByClassName(child.children); } } } function tagElementsByClassList(children) { for (let i = 0, child, cls; i < children.length; i++) { child = children[i]; child.classList.add('tagged'); if (child.firstElementChild) { tagElementsByClassList(child.children); } } }
Tests:
get/set Attribute
tagElementsByAttribute(document.getElementById('root').children);
+= className
tagElementsByClassName(document.getElementById('root').children);
classList.add()
tagElementsByClassList(document.getElementById('root').children);