Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Dataset vs getAttribute vs Property v2.1
(version: 0)
Comparing performance of:
Dataset vs getAttribute
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<h3 id="data" data-test="text" data-id="some-id">Hello world!</h3>
Script Preparation code:
var element = document.querySelector("#data");
Tests:
Dataset
const { test, id } = element.dataset; const testId = test + id;
getAttribute
const test = element.getAttribute('data-test'); const id = element.getAttribute('data-id'); const testId = test + id;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Dataset
getAttribute
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 YaBrowser/24.7.0.0 Safari/537.36
Browser/OS:
Yandex Browser 24 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Dataset
1825425.8 Ops/sec
getAttribute
3761129.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and benchmark setup to understand what's being tested. **Benchmark Definition** The benchmark is designed to compare three approaches for accessing data attributes in JavaScript: 1. `dataset` property (using the `const { test, id } = element.dataset;` syntax) 2. `getAttribute` method (using the `element.getAttribute('data-test')` and `element.getAttribute('data-id')` syntaxes) **Options Being Compared** The two options being compared are: * Using the `dataset` property (`test` and `id` accessors) to access data attributes * Using the `getAttribute` method to access data attributes (by their attribute name) **Pros and Cons of Each Approach** 1. **Dataset Property** * Pros: + More readable and concise syntax + Accesses data attributes directly, without needing an attribute name + Typically faster than `getAttribute` * Cons: + Limited to accessing data attributes that are defined using the `data-` prefix (e.g., `data-test`, `data-id`) 2. **getAttribute Method** * Pros: + More flexible, as it can access any attribute without a specific prefix + Works with attributes that don't have a `data-` prefix * Cons: + Less readable and more verbose syntax + Typically slower than accessing data attributes directly using the `dataset` property **Library Used** The benchmark doesn't explicitly mention any libraries, but it does use HTML and CSS to create an element with a `data-test` attribute. However, the `dataset` property access is specific to modern browsers (Chrome 80+, Firefox 69+), which may not be compatible with older versions. **Special JS Feature or Syntax** The benchmark uses ES6 syntax features, such as: * Template literals (`const { test, id } = element.dataset;`) * Destructuring assignment (`const { test, id } = element.dataset;`) These features are widely supported in modern browsers and Node.js environments. **Alternative Approaches** Other approaches to accessing data attributes could include: 1. Using a library like jQuery or Lodash to access data attributes 2. Defining custom getter functions for the `element` object to access data attributes 3. Using a different attribute name or prefix (e.g., `_data-test_`)
Related benchmarks:
DataAttribute vs Class Selector vs ID native Selector
dataset vs getAttribute speed
id vs getAttribute
DataAttribute vs Class Selector vs ID GetById
Comments
Confirm delete:
Do you really want to delete benchmark?