classList.add vs if !classList.contains then add (v2)

Revision 2 of this benchmark created on


Preparation HTML

<div id="test"></div>

Setup

const t = document.getElementById("test");

Teardown

t.classList.remove("test-class"); 

Test runner

Ready to run.

Testing in
TestOps/sec
classList.add
t.classList.add("test-class");
ready
!classList.contains then add
if (!t.classList.contains("test-class")) t.classList.add("test-class");
ready

Revisions

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