Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
setAttribute vs. dataset vs. direct
(version: 0)
Comparing performance of:
setAttribute vs dataset vs direct
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="foo"></div>
Tests:
setAttribute
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.setAttribute("foo", "bar"); }
dataset
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.dataset.foo = 'bar'; }
direct
var element = document.getElementById("foo"); var i = 1000; while (i--) { element.foo = 'bar'; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
setAttribute
dataset
direct
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/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
setAttribute
15977.9 Ops/sec
dataset
6983.9 Ops/sec
direct
1368313.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark is comparing three ways to set the value of an HTML element's `foo` attribute: setting it directly, using the `dataset` property, and using the `setAttribute` method. The goal is to determine which approach is the fastest. **Options Compared** 1. **Direct Setting**: This involves using the dot notation (`element.foo = 'bar';`) to set the value of the `foo` attribute directly on the element. 2. **Dataset Property**: This uses the `dataset` property (a part of the HTML5 specification) to set the value of an attribute without having to use the `setAttribute` method. 3. **setAttribute Method**: This involves using the `setAttribute` method (`element.setAttribute('foo', 'bar');`) to set the value of the `foo` attribute. **Pros and Cons** * **Direct Setting**: + Pros: Simple, intuitive, and widely supported. + Cons: May not be as efficient or optimized as other methods. * **Dataset Property**: + Pros: Efficient, modern, and part of HTML5 specification. Reduces the need for additional libraries or plugins. + Cons: Not all browsers support it out-of-the-box, and some older browsers may have limitations or issues with it. * **setAttribute Method**: + Pros: Widely supported across various browsers and versions. + Cons: May be slower than other methods due to the overhead of invoking a method. **Library/Functionality** In this benchmark, there is no explicit library being tested. However, the `dataset` property is an HTML5 feature that is widely supported by modern browsers. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax being used in this benchmark. It's standard JavaScript code using basic syntax and semantics. **Alternative Approaches** If you're interested in exploring alternative approaches to setting attribute values, here are a few: 1. **Custom Property**: You can use the `customProperties` API (also part of HTML5) to set custom properties on an element. 2. **getAttribute/setAttribute Pairing**: Some browsers have optimized the `getAttribute` and `setAttribute` methods for certain use cases. For example, some versions of Chrome use a "cache" to store attribute values that can reduce overhead when setting or getting attributes. Keep in mind that these alternatives may not be as widely supported or efficient as the approaches being tested in this benchmark.
Related benchmarks:
id vs setAttribute
getAttribute('data-foo') vs dataset.foo
Check data attribute: hasAttribute vs dataset
getAttribute vs dataset (without explicit loop)
Comments
Confirm delete:
Do you really want to delete benchmark?