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 (Windows NT 10.0; Win64; x64; rv:144.0) Gecko/20100101 Firefox/144.0
Browser:
Firefox 144
Operating system:
Windows
Device Platform:
Desktop
Date tested:
7 months ago
Test name
Executions per second
setAttribute - no if
11645.5 Ops/sec
setAttribute - with if
33238.3 Ops/sec
toggleAttribute - no if
26593.3 Ops/sec
toggleAttribute - with if
32987.5 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")