Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
getAttribute/setAttribute versus classList
(version: 0)
Comparing performance of:
setAttribute/getAttribute vs classList
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div class='testName'>TEST HERE</div>
Tests:
setAttribute/getAttribute
const el = document.querySelector('div') el.setAttribute('name', el.getAttribute('class') + ' testClass2')
classList
const el = document.querySelector('div') el.classList.add('testClass2')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
setAttribute/getAttribute
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, compared, and the pros and cons of each approach. **Benchmark Overview** The measurement is comparing two approaches to set a class on an HTML element in JavaScript: 1. Using the `setAttribute` method to directly set the class attribute. 2. Using the `classList.add()` method to add a class to the element's class list. **What's being tested?** * The performance difference between these two approaches on different browsers and platforms (desktop, Ubuntu). **Options compared:** 1. **`setAttribute`**: Sets the value of an attribute directly. 2. **`classList.add()`**: Adds a class to the element's class list. **Pros and Cons of each approach:** ### `setAttribute` Pros: * Simple and straightforward syntax * Well-documented and widely supported Cons: * Can be slower due to the overhead of setting attributes programmatically * May not be optimized for modern browsers, which often use more efficient methods ### `classList.add()` Pros: * Efficient and fast, as it leverages the browser's internal class management * Modern browsers often optimize this method for performance Cons: * Requires a DOM element to work with (e.g., using `document.querySelector`) * May not be supported in older browsers or environments where `classList` is not available **Library used:** In this benchmark, there is no explicit library mentioned. However, the use of `document.querySelector()` and `classList.add()` implies that these methods are part of the DOM API, which is a standard feature of modern web browsers. **Special JavaScript features or syntax:** None mentioned in this specific benchmark. The code uses standard JavaScript syntax for setting attributes and adding classes. **Other alternatives:** If you needed to set a class on an element without using `setAttribute` or `classList.add()`, other alternatives might include: * Using the `style` attribute directly (e.g., `el.style.class = 'newClass'`) * Setting the `className` property of the element's style object (e.g., `el.style.className = 'newClass'`) * Using a library like jQuery or another DOM manipulation library that provides similar methods Keep in mind that these alternatives might have different performance characteristics and may not be as efficient as using `classList.add()`. In summary, this benchmark measures the performance difference between two common ways to set classes on HTML elements in JavaScript. While `setAttribute` is simple and widely supported, `classList.add()` is often faster due to its optimized nature and reliance on modern browser features.
Related benchmarks:
className vs. setAttribute vs. classList
Native JS classList versus Native JS getAttribute
classList.add vs setAttribute
setAttribute vs classList.add
Comments
Confirm delete:
Do you really want to delete benchmark?