<div id="test" data-foo="bar"></div>
var testDiv = document.getElementById('test');
var foo = testDiv.getAttribute('data-foo');
var foo = testDiv.dataset.foo;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
getAttribute | |
dataset |
Test name | Executions per second |
---|---|
getAttribute | 11162648.0 Ops/sec |
dataset | 4708240.5 Ops/sec |
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:
getAttribute('data-foo')
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:
getAttribute
getAttribute()
to dynamically access HTML attributes.dataset
dataset
property of an HTML element, which is a standardized feature introduced in HTML5.Pros and Cons of each approach
Other considerations
dataset
property. However, other browsers may not support this feature.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.