Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Data attribute access
(version: 0)
Comparing performance of:
by data attribute vs by function
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id='xd' data-name="xD"></div>
Script Preparation code:
var e = (() => { const state = { name: 'xD' } const el = document.getElementById('xd') el.get_name = () => state.name return el })()
Tests:
by data attribute
const name = e.dataset.name
by function
const name = e.get_name()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
by data attribute
by function
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 dive into the explanation of the provided JavaScript microbenchmark. **What is being tested?** The benchmark measures the performance difference between two approaches to access a value stored in a HTML element: by using a data attribute (via `dataset`) and by accessing a custom function that returns the same value. **Options compared** Two options are being compared: 1. **By Data Attribute**: This approach uses the `dataset` property of an HTML element to access the value stored in the `data-name` attribute. 2. **By Function**: This approach accesses the value through a custom function `get_name()` that is defined on the same HTML element. **Pros and Cons** * **By Data Attribute**: + Pros: Simple, easy to implement, and widely supported by modern browsers. + Cons: May not be as efficient due to the need to access an attribute, which can lead to additional overhead. * **By Function**: + Pros: Can be more efficient since it avoids accessing an attribute, reducing overhead. + Cons: Requires defining a custom function on the element, which can add complexity. **Other considerations** The benchmark also considers the impact of using `const` to declare variables and the use of arrow functions (as seen in the script preparation code). **Library used (if applicable)** None is explicitly mentioned in the provided benchmark definition. However, it's worth noting that some browsers may have optimized their internal data structures or caching mechanisms for elements with a specific attribute, such as `data-` attributes. **Special JS feature or syntax** This benchmark does not use any special JavaScript features or syntax beyond what is typically available in modern browsers. It relies on standard HTML attributes and basic JavaScript functionality. **Alternatives** There are other approaches to access values stored in HTML elements: * Using `getAttribute()` and `setAttribute()`: This method is more verbose but allows for more control over the attribute's value. * Using `requestAnimationFrame()` or `setTimeout()`: These methods can be used to defer execution of code until a specific time, potentially improving performance by avoiding unnecessary computations. Keep in mind that these alternatives might not provide significant performance benefits in this particular benchmark and may introduce additional complexity.
Related benchmarks:
Dataset vs getAttribute vs Property v2
testing parent and hasAttribute
Get attribute / dataset - performance
Dataset vs getAttribute vs Property v2.1
Comments
Confirm delete:
Do you really want to delete benchmark?