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 vs Add className
Created:
8 years ago
by:
Guest
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"; }
setAttribute
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.setAttribute("class", "bar"); }
classList
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.classList.add("bar"); }
Add className
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.className += "bar"; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
className
setAttribute
classList
Add className
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 benchmark and explain what's being tested, compared options, pros and cons, library usage, special JS features or syntax (if applicable), and alternative approaches. **Benchmark Definition** The provided JSON represents a benchmark definition for "className vs. setAttribute vs. classList". This benchmark compares three different ways to set a CSS class on an HTML element: 1. `className` 2. `setAttribute` with a value of "class" 3. `classList.add` **Options Compared** The three options are compared in the following individual test cases: * **`className`**: Sets the CSS class directly on the element using the dot notation. * **`setAttribute`**: Sets an attribute named "class" and assigns a string value to it, which then sets the CSS class. * **`classList.add`**: Adds a new CSS class to the element's class list. **Pros and Cons** Here are some pros and cons of each approach: * `className`: + Pros: Simple and efficient way to set a CSS class. + Cons: Can be prone to typos if not used carefully (e.g., "bar" instead of "Bar"). * `setAttribute`: + Pros: Allows for more control over the attribute's value and usage. + Cons: May lead to unnecessary attribute creation, which can have performance implications. * `classList.add`: + Pros: More modern and efficient way to manage CSS classes, as it avoids creating unnecessary attributes. + Cons: Requires JavaScript runtime support (modern browsers). **Library Usage** None of the test cases explicitly use a library. However, it's worth noting that the `classList` API is supported by most modern browsers, which makes it a viable option for setting CSS classes. **Special JS Features or Syntax** The benchmark uses a simple JavaScript loop (`while (i--)`) to execute the code multiple times. This allows for timing measurements and comparisons between the different approaches. **Alternative Approaches** Some alternative ways to set a CSS class include: * Using a string interpolation method, such as `element.classList.add(`${class}`)`. * Using a library like jQuery or Lodash to manipulate the element's class list. * Using a CSS-in-JS solution like Emotion or styled-components. These alternatives may offer additional benefits, such as improved performance, easier maintenance, or more flexible styling options. However, they also introduce new dependencies and potential performance overheads.
Related benchmarks:
className vs. setAttribute vs. classList
Native JS classList versus Native JS getAttribute
classList.add vs setAttribute
DataAttribute vs Class Selector on body
setAttribute vs classList.add
Comments
Confirm delete:
Do you really want to delete benchmark?