Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
className vs. setAttribute vs. classList (static and dynamic classes)
(version: 0)
Comparing performance of:
className vs setAttribute vs classList
Created:
7 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="foo"></div>
Tests:
className
var element = document.getElementById("foo"); element.className = "bar"; var i = 1000; while (i--) { element.className = "bar " + i; }
setAttribute
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.setAttribute("class", "bar " + i); }
classList
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.classList.add(i); element.classList.remove(i + 1); }
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 benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares three approaches for setting class names on an HTML element: 1. `className` property (with static classes) 2. `setAttribute` method 3. `classList` API (with dynamic classes) **Options Compared** Here are the options being compared, along with their pros and cons: ### 1. `className` Property * **How it works:** Setting a class name using the `className` property is done by assigning a string value to the `className` attribute of an element. In this benchmark, static classes (i.e., not dynamically generated) are used. * **Pros:** + Fast and efficient + Simple to implement * **Cons:** + Limited flexibility (can't easily remove or modify individual classes) + Can lead to slower performance if the class name is very long ### 2. `setAttribute` Method * **How it works:** The `setAttribute` method sets a named attribute on an element, in this case, the `class` attribute. * **Pros:** + Flexible (can set multiple attributes at once) + Wide browser support * **Cons:** + Slower than the `className` property + Requires more code to implement ### 3. `classList` API * **How it works:** The `classList` API is a modern feature that allows you to add, remove, and modify classes on an element. * **Pros:** + Flexible (can easily remove or modify individual classes) + Modern feature with good browser support * **Cons:** + Slower than the `className` property + Not supported in older browsers **Library/ API** None of these options rely on a specific library or API. **Special JS Feature/Syntax** The `classList` API is a modern JavaScript feature, introduced in ECMAScript 2015 (ES6). It's not widely supported in older browsers, but it provides a convenient way to manage classes on an element. **Other Alternatives** If you need more flexibility or better performance than the options compared here, you may consider using: * `style` attribute: You can set CSS styles directly on an element using the `style` attribute. While this doesn't solve the class management problem, it provides a way to apply styles that might be useful in some cases. * Custom solution: Depending on your specific requirements, you might need to implement a custom solution using JavaScript and CSS to manage classes on an element. Keep in mind that these alternatives will likely have different performance characteristics than the options compared here.
Related benchmarks:
className vs. setAttribute vs. classList
classList.add vs setAttribute
className vs. setAttribute vs. classList - exclusive
setAttribute vs classList.add
Comments
Confirm delete:
Do you really want to delete benchmark?