Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Conditional and unconditional setAttribute vs toggleAttribute
Compares the performance of conditional (if-wrapped) and unconditional setAttribute("attr", "") vs toggleAttribute("attr",true)
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0
Browser:
Firefox 140
Operating system:
Linux
Device Platform:
Desktop
Date tested:
one month ago
Test name
Executions per second
setAttribute - no if
4000.9 Ops/sec
setAttribute - with if
14457.5 Ops/sec
toggleAttribute - no if
10510.8 Ops/sec
toggleAttribute - with if
14445.9 Ops/sec
HTML Preparation code:
<div id="test"></div>
Script Preparation code:
var test_element = document.querySelector("#test")
Tests:
setAttribute - no if
for(let i = 0; i < 1000; i++){ test_element.setAttribute("asd","") } test_element.removeAttribute("asd")
setAttribute - with if
for(let i = 0; i < 1000; i++){ if(!test_element.hasAttribute("asd")){test_element.setAttribute("asd","")} } test_element.removeAttribute("asd")
toggleAttribute - no if
for(let i = 0; i < 1000; i++){ test_element.toggleAttribute("asd",true) } test_element.removeAttribute("asd")
toggleAttribute - with if
for(let i = 0; i < 1000; i++){ if(!test_element.hasAttribute("asd")){test_element.toggleAttribute("asd",true)} } test_element.removeAttribute("asd")