Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
getAttribute('data-foo') vs dataset.foo
(version: 0)
Comparing performance of:
getAttribute vs dataset
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="test" data-foo="bar"></div>
Script Preparation code:
var testDiv = document.getElementById('test');
Tests:
getAttribute
var foo = testDiv.getAttribute('data-foo');
dataset
var foo = testDiv.dataset.foo;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
getAttribute
dataset
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
getAttribute
38623884.0 Ops/sec
dataset
20607288.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, the options compared, pros and cons of each approach, and other considerations. **What's being tested?** The benchmark measures the performance difference between two ways to access HTML attribute data: 1. `getAttribute('data-foo')` 2. `dataset.foo` These two approaches are used to retrieve the value of an HTML attribute called "data-foo" from a `<div>` element with the id "test". **Options compared** There are two test cases: 1. `getAttribute` * This approach uses the built-in JavaScript method `getAttribute()` to dynamically access HTML attributes. 2. `dataset` * This approach uses the `dataset` property of an HTML element, which is a standardized feature introduced in HTML5. **Pros and Cons of each approach** 1. **getAttribute()** + Pros: - Widespread support across browsers (even older ones). - Simple to use. + Cons: - May be slower due to the overhead of parsing the attribute name and value. 2. **dataset** * Pros: - Faster, as it bypasses the need for attribute parsing. - More readable and expressive code. + Cons: - Requires HTML5 support (not available in older browsers). - Less intuitive for non-HTML5 developers. **Other considerations** 1. **Browser limitations**: The benchmark is running on Firefox 119, which supports the `dataset` property. However, other browsers may not support this feature. 2. **HTML attribute implementation**: Some browsers may implement attributes in different ways, affecting performance. For example, Internet Explorer uses a proprietary implementation called "data" instead of "dataset". 3. **Optimization and caching**: Browsers can optimize and cache frequently accessed data, including HTML attributes. **Library and its purpose** The `dataset` property is not a library; it's a standardized feature introduced in HTML5. It provides an easy way to access attribute data without having to use `getAttribute()` or other workarounds. **Special JS feature or syntax** There are no special features or syntax used in this benchmark. However, it's worth noting that the `dataset` property is a shorthand for accessing attribute data, and its usage can be seen as a simplification of more traditional approaches. In conclusion, the benchmark measures the performance difference between two ways to access HTML attribute data: using `getAttribute()` and accessing attributes through the `dataset` property. While `getAttribute()` has wider browser support, the `dataset` approach is faster and more readable, but requires HTML5 support and may not work in older browsers.
Related benchmarks:
id vs getAttribute
Check data attribute: hasAttribute vs dataset
DataAttribute vs Class Selector vs ID GetById
getAttribute vs dataset (without explicit loop)
Comments
Confirm delete:
Do you really want to delete benchmark?