Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
hasAttribute vs classList
(version: 0)
Comparing performance of:
hasAttribute vs classList
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<p id="foo" class="foo" data-foo></p>
Tests:
hasAttribute
const el = document.getElementById("foo"); let i = 10000; while (i--) { if (el.classList.contains('foo')) { const foo = 'foo'; } }
classList
const el = document.getElementById("foo"); let i = 10000; while (i--) { if (el.hasAttribute("data-foo")) { const foo = 'foo'; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
hasAttribute
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 JSON data for the JavaScript microbenchmark. **Benchmark Overview** The benchmark is designed to compare two approaches for checking if an element has a specific attribute: using `classList.contains()` and using `hasAttribute()`. The goal is to determine which approach is faster, more efficient, or both. **Options Being Compared** Two options are being compared: 1. **`classList.contains('foo')`**: This method checks if the element with the ID "foo" has a class attribute named "foo". 2. **`el.hasAttribute("data-foo")`**: This method checks if the element with the ID "foo" has an attribute named "data-foo". **Pros and Cons of Each Approach** 1. **`classList.contains('foo')`** * Pros: + More intuitive and readable, as it explicitly specifies what class to check for. + May be faster if the element's class list is large, as it only needs to iterate over a subset of classes. * Cons: + Can be slower on smaller class lists or when iterating over multiple classes, due to the method's overhead. 2. **`el.hasAttribute("data-foo")`** * Pros: + Faster, as it only checks if the attribute exists without requiring class iteration. * Cons: + Less intuitive and less readable, as it requires understanding of the `hasAttribute()` method's behavior. + May be slower on elements with many attributes due to the method's overhead. **Library Used** The benchmark uses the `classList` API, which is a part of the HTML5 specification. The `classList` property returns a `DOMTokenList` object that provides methods for manipulating class lists. In this case, `contains()` is used to check if an element has a specific class. **Special JavaScript Feature/Syntax** The benchmark uses a modern JavaScript feature: template literals (`\r\nconst foo = 'foo';\r\n`). Template literals are a way to create strings with embedded expressions using the `\` character. This feature was introduced in ECMAScript 2015 (ES6) and is widely supported by modern browsers. **Other Alternatives** If you want to compare these approaches, you could also consider: 1. Using `el.getAttribute("data-foo")` instead of `el.hasAttribute("data-foo")`. This would allow you to check if the attribute has a specific value. 2. Implementing your own class iteration solution using `for...in` or `forEach()` loops. 3. Comparing performance with different browser versions, as older browsers may have different optimizations for these methods. By considering these alternatives, you can gain a deeper understanding of the trade-offs involved in choosing between these approaches and optimize your code accordingly.
Related benchmarks:
classList.contains vs. hasAttribute
classList.add vs setAttribute
DataAttribute vs Class Selector on body
Check data attribute: hasAttribute vs dataset
Comments
Confirm delete:
Do you really want to delete benchmark?