Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
getAttribute vs dataset (without explicit loop)
(version: 0)
Compare setAttribute to dataset
Comparing performance of:
getAttribute vs dataset
Created:
one year 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 foo = element.getAttribute("data-foo");
dataset
var element = document.getElementById("foo"); 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:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Browser/OS:
Chrome 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
getAttribute
26810748.0 Ops/sec
dataset
16590174.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark is designed to compare two approaches for accessing data in HTML elements: `getAttribute` and `dataset`. The script preparation code is empty, which means that the JavaScript code will be run on a clean slate. The HTML preparation code creates a simple `<div>` element with an ID "foo" and a `data-foo` attribute set to "foo_id". **Individual Test Cases** There are two test cases: 1. **Test Case 1: `getAttribute`** The benchmark definition is: ```javascript var element = document.getElementById("foo"); var foo = element.getAttribute("data-foo"); ``` This test case uses the `getAttribute` method to access the value of the `data-foo` attribute on the `foo` element. 2. **Test Case 2: `dataset`** The benchmark definition is: ```javascript var element = document.getElementById("foo"); var foo = element.dataset.foo; ``` This test case uses the `dataset` property to access the value of the `foo` attribute on the `foo` element. **Options Compared** The two approaches are compared in terms of performance and speed. The benchmark measures how many executions per second (ExecutionsPerSecond) each approach can handle. **Pros and Cons** * **getAttribute**: This method has the advantage of being a standard DOM method that's widely supported across browsers. However, it requires an explicit lookup of the attribute name, which can lead to slower performance compared to other approaches. * **dataset**: This method is a newer feature introduced in HTML5 that provides a more concise way to access attributes as properties on the element object. It's also faster than `getAttribute` because it doesn't require an extra lookups. **Library and Purpose** Neither of these methods rely on any external libraries, but they do utilize the DOM (Document Object Model) API, which is a fundamental part of JavaScript programming. **Special JS Feature or Syntax** This benchmark uses a feature that's specific to modern browsers: `dataset`. The `dataset` property is not supported in older browsers like Internet Explorer. This means that if you're targeting older browsers, you may need to use the `getAttribute` method instead. **Other Alternatives** If you needed to access attributes on an element without using `getAttribute` or `dataset`, you might consider other approaches: * Using a library like jQuery, which provides its own way of accessing attributes through the `data()` method. * Writing custom JavaScript code that uses the DOM API to access attributes. However, these alternatives are less common and may not be as performant as using the built-in `getAttribute` or `dataset` methods.
Related benchmarks:
getAttribute vs dataset 2
hasAttribute vs dataset
Check data attribute: hasAttribute vs dataset
hasAttribute vs getAttribute vs dataset
Comments
Confirm delete:
Do you really want to delete benchmark?