Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
classList vs setAttribute
(version: 0)
Comparing performance of:
classList vs setProperty
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="foo" class="lorem"></div>
Tests:
classList
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.classList.add("bar"); }
setProperty
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.setAttribute("data-foo", "bar"); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
classList
setProperty
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 its test cases to understand what's being tested. **Benchmark Overview** The benchmark, named "classList vs setAttribute", compares the performance of two approaches: using the `classList` property versus setting an attribute on an HTML element. The goal is to determine which approach is faster in this specific scenario. **Options Compared** There are only two options being compared: 1. **`classList`**: This method adds a class to the element without specifying any additional attributes. 2. **`setAttribute`**: This method sets an attribute on the element with the specified value. **Pros and Cons of Each Approach** * **`classList`**: + Pros: More efficient, as it doesn't create a new attribute, reducing overhead. + Cons: May not be supported in older browsers or environments that don't understand class lists. * **`setAttribute`**: + Pros: Widely supported across browsers and environments. + Cons: Creates a new attribute, which can lead to performance overhead. **Library Usage** In the benchmark preparation code, there is no library usage. The code only uses standard JavaScript and DOM APIs. **Special JS Feature or Syntax** There are no special JS features or syntax used in this benchmark that would require additional explanation. **Other Alternatives** In theory, other approaches could be compared to `classList` and `setAttribute`, such as: * Using a different attribute name (e.g., `data-foo`) * Using a CSS class instead of an attribute * Using a custom JavaScript function or library However, these alternatives are not currently being tested in this benchmark. **Benchmark Preparation Code** The provided HTML preparation code creates a simple `<div>` element with the ID "foo" and an empty `class` attribute: ```html <div id="foo" class="lorem"></div> ``` This is used as a starting point for both test cases, which then perform the respective operations (adding a class using `classList` or setting an attribute using `setAttribute`).
Related benchmarks:
className vs. setAttribute vs. classList
className vs. setAttribute vs. classList (fixed syntax error)
classList.add vs setAttribute
DataAttribute vs Class Selector on body
setAttribute vs classList.add
Comments
Confirm delete:
Do you really want to delete benchmark?