<div id="foo" data-foo="foo_id"></div>
var element = document.getElementById("foo");
var i = 10000;
while (i--) {
var foo = element.getAttribute("data-foo");
}
var element = document.getElementById("foo");
var i = 10000;
while (i--) {
var foo = element.dataset.foo;
}
var element = document.getElementById("foo");
var i = 10000;
while (i--) {
var foo = element.hasAttribute("data-foo");
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
getAttribute | |
dataset | |
has attr |
Test name | Executions per second |
---|---|
getAttribute | 156545.6 Ops/sec |
dataset | 1274.1 Ops/sec |
has attr | 203044.3 Ops/sec |
Benchmark Overview
The MeasureThat.net benchmark compares three approaches to accessing attribute values in HTML elements: getAttribute
, dataset
, and hasAttribute
. The test cases are designed to measure the performance differences between these approaches on a specific webpage.
Test Cases
There are three individual test cases:
getAttribute
: This test case uses the getAttribute()
method to access the value of the data-foo
attribute in an HTML element.dataset
: This test case uses the dataset
property to access the value of the data-foo
attribute in an HTML element.hasAttribute
: This test case uses the hasAttribute()
method to check if an attribute is present on an HTML element, and then accesses the attribute value using the getAttribute()
method.Options Compared
The benchmark compares the following options:
getAttribute
: Accessing attribute values using the getAttribute()
method.dataset
: Using the dataset
property to access attribute values.hasAttribute
: Checking if an attribute is present on an element and then accessing its value using getAttribute
.Pros and Cons of Each Approach
getAttribute
:dataset
:getAttribute
.hasAttribute
: getAttribute
, then accessing its value. However, it still uses the same CPU cycles to access the attribute value as getAttribute
.dataset
, requires two steps (checking and then accessing).Library Usage
None of the test cases use any external libraries.
Special JS Feature or Syntax
dataset
: This property was introduced in HTML5 and allows for more convenient access to attribute values using dot notation (element.dataset.foo
).hasAttribute()
: This method was introduced in ECMAScript 2019 (ES2020) as a way to check if an attribute is present on an element without having to use getAttribute()
explicitly.Other Alternatives
If you need more performance or want to explore other approaches, consider the following:
setAttribute()
: Instead of accessing attribute values, you can set attributes using setAttribute()
. This might be faster than accessing attribute values in some cases.dataset
-like properties: Some libraries (e.g., jQuery) provide their own dataset
-like properties for older browsers. However, these may not offer the same performance as native HTML5 dataset
support.Keep in mind that browser support and performance can vary greatly depending on the specific test case and implementation details. MeasureThat.net's benchmark results should be used to inform your decision-making process when choosing between these approaches.