Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
setAttribute vs. classList
(version: 0)
Comparing performance of:
classList add vs classList toggle vs setAttribute
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="foo"></div>
Tests:
classList add
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.classList.add("bar"); }
classList toggle
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.classList.toggle("bar"); }
setAttribute
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.setAttribute('active', true); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
classList add
classList toggle
setAttribute
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 what's being tested in the provided benchmark. The benchmark compares three approaches to modify an HTML element's class list: 1. `classList.add()` (adding a class) 2. `classList.toggle()` (toggling a class) 3. `setAttribute('active', true)` (setting a custom attribute) **Options compared:** * The three methods for modifying the `classList` property. * These methods are used to add or remove classes from an HTML element. **Pros and Cons of each approach:** 1. **`classList.add()`**: * Pros: This method is native to modern browsers, efficient, and easy to read. * Cons: Some older browsers may not support this method, which can lead to compatibility issues. 2. **`classList.toggle()`**: * Pros: Similar to `classList.add()`, but also allows for removing classes by passing the class name with no argument (e.g., `element.classList.toggle('bar')`). This method is also efficient and easy to read. * Cons: The main difference between this and `classList.add()` is that it toggles the presence of a class, which can be misleading if not used carefully. 3. **`setAttribute('active', true)`**: * Pros: This method uses a standard attribute approach, which might be familiar to developers who come from other programming backgrounds (e.g., setting a property on an object). * Cons: This method is less efficient and more verbose than `classList.add()` or `classList.toggle()`, making it less suitable for performance-critical code. **Library and purpose:** There is no explicit library mentioned in the benchmark definition. However, it's worth noting that `classList` is a part of the HTML5 DOM API, which has been widely adopted by modern browsers. **Special JS feature or syntax:** None are explicitly mentioned in this benchmark. **Other considerations:** When choosing between these methods, consider factors like: * Browser support: If you need to support older browsers, `setAttribute()` might be a better choice. * Performance: For performance-critical code, `classList.add()` and `classList.toggle()` are likely to be faster due to their native implementation. * Code readability: Choose the method that best fits your coding style and team conventions. **Alternatives:** If you need to support even older browsers or have specific requirements not covered by this benchmark, consider using other methods: * For adding classes on Internet Explorer (IE): Use `element.className += 'bar';`. * For removing classes on IE: Use `element.className = element.className.replace(' bar', '');` (note the space at the end of the class name). * For setting custom attributes on older browsers: Use `element.setAttribute('custom-attribute', 'value');`. Keep in mind that these alternatives might have different performance characteristics and may not be as efficient as the methods tested in this benchmark.
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?