Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
getAttribute vs dataset
(version: 0)
Compare setAttribute to dataset
Comparing performance of:
getAttribute vs dataset
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="foo" data-foo="foo_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; }
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:
one month ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
getAttribute
5298.9 Ops/sec
dataset
2742.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its options. **Benchmark Overview** The benchmark compares two ways to access data attributes in HTML elements: `getAttribute` and `dataset`. The goal is to determine which method is faster. **Script Preparation Code** There is no script preparation code provided for this benchmark. This means that the test starts with a clean slate, without any additional JavaScript code that might affect the results. **HTML Preparation Code** The HTML preparation code creates a simple element with a `data-foo` attribute: ``` <div id="foo" data-foo="foo_id"></div> ``` This element will be used to test both `getAttribute` and `dataset`. **Individual Test Cases** There are two test cases: 1. **`getAttribute`**: This test case uses the `getAttribute` method to access the `data-foo` attribute. The benchmark defines a while loop that iterates 10,000 times, accessing the attribute on each iteration. 2. **`dataset`**: This test case uses the `dataset` property to access the same `data-foo` attribute. Again, the benchmark defines a while loop that iterates 10,000 times, accessing the attribute on each iteration. **Pros and Cons of Each Approach** * **`getAttribute`**: + Pros: widely supported, simple syntax. + Cons: can be slower than `dataset` for large datasets, as it involves parsing the attribute string. * **`dataset`**: + Pros: faster for large datasets, more efficient parsing. + Cons: requires browser support (introduced in HTML5), slightly less widely supported. **Library/Technique Used** In this benchmark, no libraries or techniques beyond standard JavaScript are used. The `dataset` property is a built-in feature of the DOM API. **Special JS Features/Syntax** None mentioned explicitly. However, it's worth noting that some older browsers may not support the `dataset` property or have slightly different behavior when using `getAttribute`. **Other Alternatives** If you want to compare other methods for accessing data attributes in HTML elements, you could consider: * Using a library like jQuery, which provides a more convenient API for accessing attributes (e.g., `.attr()`). * Using a custom implementation, such as parsing the attribute string or using regular expressions. * Comparing other approaches, such as using CSS to define the attribute values and then querying them in JavaScript.
Related benchmarks:
getAttribute('data-foo') vs dataset.foo
getAttribute vs dataset 2
hasAttribute vs dataset
Check data attribute: hasAttribute vs dataset
getAttribute vs dataset (without explicit loop)
Comments
Confirm delete:
Do you really want to delete benchmark?