Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
getAttribute vs dataset gregdaynes destructure
(version: 0)
Compare setAttribute to dataset
Comparing performance of:
getAttribute vs dataset vs dataset x2 vs dataset destructure
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="foo" data-foo="foo_id" data-bar="bar_id" ></div>
Tests:
getAttribute
var element = document.getElementById("foo"); var i = 10000; while (i--) { var foo = element.getAttribute("data-foo"); }
dataset
var element = document.getElementById("foo"); var i = 10000; while (i--) { var foo = element.dataset.foo; }
dataset x2
var element = document.getElementById("foo"); var i = 10000; while (i--) { var foo = element.dataset.foo; var bar = element.dataset.bar; }
dataset destructure
var element = document.getElementById("foo"); var i = 10000; while (i--) { var { foo, bar } = element.dataset; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
getAttribute
dataset
dataset x2
dataset destructure
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:130.0) Gecko/20100101 Firefox/130.0
Browser/OS:
Firefox 130 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
getAttribute
156198.2 Ops/sec
dataset
2104.6 Ops/sec
dataset x2
849.5 Ops/sec
dataset destructure
1102.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what's being tested, compared, and considered. **Benchmark Definition** The benchmark is designed to compare three approaches for accessing attribute values from an HTML element: 1. `getAttribute`: Using the `getAttribute()` method to retrieve a specific attribute value. 2. `dataset` (without deconstruction): Accessing attribute values directly using the `.dataset` property. 3. `dataset x2` (with deconstruction, twice): Same as above, but with the `.dataset` property accessed twice, and then destructured into two variables. **Comparison Options** The three approaches being compared are: 1. **`getAttribute()`**: This method retrieves a specific attribute value from an element. * Pros: + Well-established and widely supported. + Provides more control over the attribute retrieval process. * Cons: + May be slower due to the overhead of calling a separate method. + Limited to accessing single attributes only. 2. **`dataset` (without deconstruction)**: This approach accesses attribute values directly using the `.dataset` property, which returns an object containing all attribute values as key-value pairs. * Pros: + Faster than `getAttribute()` since it doesn't involve a separate method call. + More concise and easier to read for simple use cases. 3. **`dataset x2` (with deconstruction)**: This approach accesses `.dataset` twice, and then destructures the resulting object into two variables using destructuring assignment (`{ foo, bar } = element.dataset;`). * Pros: + Provides more flexibility than `dataset` alone, as it allows for multiple attribute retrievals. * Cons: + May be slower due to the overhead of calling `.dataset()` twice. + Requires more code to achieve the same result. **Library: None** There are no external libraries being used in this benchmark. **Special JS Feature/Syntax** None mentioned, but note that deconstruction assignment (`{ foo, bar } = element.dataset;`) is a feature introduced in ECMAScript 2015 (ES6). **Other Considerations** When choosing between these approaches, consider the following factors: * Performance: If speed is critical, `dataset` without deconstruction might be the fastest option. However, if you need to access multiple attributes or prefer more control over attribute retrieval, `getAttribute()` might be a better choice. * Code Readability and Maintainability: For simple cases, `dataset` without deconstruction can be more concise and readable. However, for more complex use cases or when working with multiple attributes, `getAttribute()` or the `dataset x2` approach might be more suitable. * Browser Support: All three approaches are supported by most modern browsers. However, older browsers may not support the newer features like `.dataset`. **Alternative Approaches** Other alternative approaches to accessing attribute values include: 1. Using HTML attributes directly (e.g., `element.getAttribute("data-foo")`). 2. Using a library or framework that provides an optimized way of accessing attribute values. 3. Implementing a custom solution using the DOM manipulation API. Keep in mind that these alternatives might offer better performance, readability, or maintainability for specific use cases, but may also introduce additional complexity or dependencies.
Related benchmarks:
getAttribute('data-foo') vs dataset.foo
getAttribute vs dataset 2
Check data attribute: hasAttribute vs dataset
getAttribute vs dataset gregdaynes destructure multiple getAttribute
getAttribute vs dataset (without explicit loop)
Comments
Confirm delete:
Do you really want to delete benchmark?