Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
className vs. setAttribute vs. classList many
(version: 0)
Comparing performance of:
className vs setAttribute vs classList
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="foo" class="bar1 bar3 bar4 bar6"></div>
Tests:
className
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.className = "bar1 bar2 bar3 bar4 bar5 bar6"; element.className = "bar1 bar3 bar4 bar6"; }
setAttribute
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.setAttribute("class", "bar1 bar2 bar3 bar4 bar5 bar6"); element.setAttribute("class", "bar1 bar3 bar4 bar6"); }
classList
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.classList.add("bar2", "bar5"); element.classList.remove("bar2", "bar5"); }
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):
Let's break down the provided benchmark and explain what's being tested, along with their pros and cons. **Benchmark Test Cases:** The benchmark tests three different approaches to update the class list of an HTML element: 1. **`className`**: This approach uses the `className` property to set the value of the `class` attribute. 2. **`setAttribute`**: This approach uses the `setAttribute` method to set the value of the `class` attribute. 3. **`classList`**: This approach uses the `classList` API to add and remove classes from the element. **Library Usage:** None of these test cases use any external libraries, but they do rely on built-in JavaScript features. **Special JS Features or Syntax:** The test cases use a feature called " closure" which is used in the while loop, where we can declare variables inside a block scope that are not accessible outside the block. This is a JavaScript concept. **Pros and Cons of each approach:** 1. **`className`**: * Pros: Simple, easy to read, and well-supported by most browsers. * Cons: Can be slower due to the need to parse the `class` attribute value, which can contain multiple classes separated by spaces. 2. **`setAttribute`**: * Pros: Fast, as it doesn't require parsing or splitting the class string. * Cons: Requires using a method call and may not be as readable as the `className` approach. 3. **`classList`**: * Pros: Modern and efficient, as it uses a more recent API that is designed for managing classes. * Cons: Not supported by older browsers, which may limit its use. **Other alternatives:** For setting class names on an element, you could also consider using CSS classes with the `class` attribute or inline styles. However, these approaches are less efficient and may not be as flexible as the three test cases. Here's a brief comparison: * Using CSS classes: `element.classList.add("bar1");` would add a class to the element. * Using inline styles: `element.style="background-color: red;"` would set a style on the element, but this approach is less flexible and may not be suitable for all use cases. Keep in mind that the choice of approach depends on your specific requirements, such as performance, compatibility with older browsers, or ease of maintenance.
Related benchmarks:
className vs. setAttribute vs. classList
setAttribute vs classList.add (attribute adding) V2
setAttribute(...) vs. classList.add(...) vs classList.value vs className (multiple classes, no override)
classList.add vs setAttribute
setAttribute vs classList.add
Comments
Confirm delete:
Do you really want to delete benchmark?