Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
className vs. setAttribute() vs. classList.add() vs classList.value
(version: 0)
Comparing performance of:
className vs setAttribute() vs classList.add() vs classList.value
Created:
5 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.add()
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.classList.add("bar"); }
classList.value
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.classList.value = "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()
classList.value
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):
Let's dive into the provided benchmark and explain what's being tested, compared options, pros/cons of each approach, library usage, special JS features or syntax, and alternatives. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark on MeasureThat.net. The benchmark tests four different approaches for setting a CSS class on an HTML element: 1. `className` 2. `setAttribute("class", "bar")` 3. `classList.add("bar")` 4. `classList.value = "bar"` **Options Compared** The options being compared are: * **`className`**: Using the dot notation to set a CSS class directly on the element. * **`setAttribute("class", "bar")`**: Using the `setAttribute()` method to set a custom attribute on the element and then parsing it as a CSS class. * **`classList.add("bar")`**: Using the `classList` property to add a CSS class to the element. * **`classList.value = "bar"`**: This approach is not valid, as `classList` does not have a `value` property. It's likely a mistake or an invalid test case. **Pros and Cons** 1. **`className`**: * Pros: Simple and efficient. * Cons: May be slower due to the repeated dot notation calls. 2. **`setAttribute("class", "bar")`**: * Pros: Can set multiple classes at once. * Cons: May be slower due to parsing the attribute value as a CSS class, which can lead to slower performance on modern browsers. 3. **`classList.add("bar")`**: * Pros: Efficient and concise. * Cons: Requires support for the `classList` property, which may not be available in older browsers. **Library Usage** None of the test cases use a specific JavaScript library. However, some modern browsers have introduced new APIs that might affect performance, such as WebAssembly (WASM) or CSS Grid/Flexbox. **Special JS Features or Syntax** There is no special JavaScript feature or syntax used in these benchmark definitions. The focus is on comparing different approaches for setting a CSS class. **Alternatives** If you want to compare alternative approaches, you could consider adding test cases for: * Using `style` property instead of `className`: `element.style.backgroundColor = "bar";` * Using `DOMTokenList` API: `element.style.classList.add("bar");` * Using a library like jQuery or Lodash * Using a CSS-in-JS solution like styled-components or Emotion Keep in mind that these alternative approaches might introduce additional complexity and may not be representative of real-world use cases. For the provided benchmark, it's essential to note that the `classList.value` approach is invalid, so it's best to remove this test case or replace it with a valid alternative.
Related benchmarks:
className vs. setAttribute vs. classList
setAttribute vs classList.add (attribute adding) V2
setAttribute(...) vs. classList.add(...) vs classList.value vs className
classList.add vs setAttribute
setAttribute vs classList.add
Comments
Confirm delete:
Do you really want to delete benchmark?