Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
className+ vs. setAttribute+ vs. classList+
(version: 0)
Comparing performance of:
className vs setAttribute vs classList
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div id="foo"></div>
Tests:
className
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.className += "bar"+String(i); }
setAttribute
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.setAttribute("class", "bar"+String(i)); }
classList
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.classList.add("bar"+String(i)); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
className
setAttribute
classList
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided JSON and benchmark explanation to help understand what's being tested. **Benchmark Definition** The benchmark is testing three different approaches for adding a class to an HTML element: 1. `className`: Using the dot notation (`element.className += "bar" + String(i);`) 2. `setAttribute`: Using the `setAttribute` method (`element.setAttribute("class", "bar" + String(i));`) 3. `classList`: Using the `classList.add()` method (`element.classList.add("bar" + String(i));`) **Options Compared** The benchmark is comparing the performance of these three approaches: * **Pros and Cons:** + `className`: - Pros: Simple, widely supported. - Cons: May be slower due to DOM manipulation. + `setAttribute`: - Pros: Fast, efficient way to set attributes. - Cons: Not designed for class names; may lead to attribute name collisions. + `classList`: - Pros: Modern, efficient, and well-supported in modern browsers. - Cons: May require additional library support or polyfills. * **Other Considerations:** + The benchmark does not account for the potential impact of class names on CSS layout or styling. If performance-critical, using `classList` might be a better choice due to its optimized DOM update behavior. **Library and Special JS Features** The test cases use the following library: * `classList`: This is a modern JavaScript API introduced in HTML5, which provides an efficient way to manage classes on elements. The `classList.add()` method is used here, which appends the specified class name to the element's class list. **Special JS Feature** There are no special JS features or syntax used in these benchmark cases. They only utilize standard JavaScript and DOM manipulation techniques. **Alternatives** Other alternatives for adding a class to an HTML element could include: * Using `style` attribute: `element.style.cssText += "bar" + String(i);` (not recommended due to performance and browser compatibility issues) * Using a library like jQuery or Lodash, which provide their own implementations of class manipulation (can be slower than native methods) Keep in mind that these alternatives are not part of the standard JavaScript API and may have different performance characteristics. In summary, the benchmark is testing three approaches for adding classes to HTML elements: `className`, `setAttribute`, and `classList`. The choice of approach depends on trade-offs between simplicity, efficiency, and browser compatibility.
Related benchmarks:
className vs. setAttribute vs. classList
className vs. setAttribute vs. classList
setAttribute vs classList.add (attribute adding) V2
classList.add vs setAttribute
setAttribute vs classList.add
Comments
Confirm delete:
Do you really want to delete benchmark?