Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
jQuery .data() vs jQuery .attr()
(version: 0)
Comparing performance of:
jQuery .data() vs jQuery .attr()
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div id="data" data-test="text">some text</div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>
Tests:
jQuery .data()
$(document).ready(function() { $("h3#data").data("test"); });
jQuery .attr()
$(document).ready(function() { $("h3#data").attr("data-test"); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
jQuery .data()
jQuery .attr()
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help explain the benchmark. **What is being tested?** The provided benchmark tests two different approaches for setting and getting data attributes on HTML elements using jQuery: `.data()` and `.attr()`. The test creates an HTML element with a `data-test` attribute, and then uses jQuery to set this attribute using both methods. The benchmark measures the execution time of each method. **Options being compared** The two options being compared are: 1. **`.data()`**: A jQuery method that sets and gets custom data attributes on HTML elements. 2. **`.attr()`**: A jQuery method that sets and gets attributes on HTML elements, including data attributes. **Pros and Cons of each approach:** * **`.data()`**: Pros: + More suitable for storing small amounts of custom data that is not meant to be persisted across page loads or document changes. + Can be used with the `$().data()` shortcut method. * Cons: + May have performance implications when used extensively, as it requires jQuery to store and retrieve the data attribute on each element. + Does not support retrieving attributes using `.attr()` directly; if you need to access a data attribute from outside a jQuery object, you must use `$().data()`. * **`.attr()`**: Pros: + Can be used to set and get any attribute on an HTML element, including standard attributes like `href` or `class`, as well as custom data attributes. + Supports retrieving attributes using `.attr()` directly; if you need to access a value from outside a jQuery object, you can use `$().attr()`. * Cons: + Can be slower than `.data()` for setting and getting large amounts of data, since it requires jQuery to parse and store the attribute value. + Does not provide the same level of encapsulation as `.data()`; attributes set with `.attr()` are visible to other scripts that have access to the HTML element. **Other considerations:** * **Browser differences:** The benchmark measures execution times on a specific browser (Chrome 111) and device platform (Desktop). Different browsers may exhibit different performance characteristics for these methods. * **JavaScript engine:** The benchmark assumes a JavaScript engine that is optimized for web development. Other engines, such as those used in Node.js or server-side rendering environments, may behave differently. **Library and special features:** The test uses jQuery as the library. jQuery provides several features that make it suitable for this type of benchmarking: * **$.data()**: A shortcut method that allows setting and getting custom data attributes on HTML elements. * **$.attr()**: A method that can be used to set and get any attribute on an HTML element, including standard attributes and custom data attributes. **Other alternatives:** If you want to test alternative approaches for setting and getting data attributes on HTML elements, some options include: * Using a plain JavaScript object to store and retrieve data attributes, rather than relying on jQuery's `.data()` method. * Using a library like Moment.js or Lodash to simplify the process of working with dates and custom data attributes. However, these alternatives may not provide the same level of encapsulation and convenience as jQuery's `.data()` method.
Related benchmarks:
Modern dataset vs old .getAttribute() vs jQuery .data() vs jQuery .attr()
Modern dataset vs old .getAttribute() vs jQuery .data() vs jQuery .attr() rfgonzalezweb
Modern dataset vs old .getAttribute() vs jQuery .data() vs jQuery .attr() 2
dataset vs getAttribute() vs jQuery.data() vs jQuery.attr()
Modern dataset vs old .getAttribute() vs jQuery .data() vs jQuery .attr() with console.log
Comments
Confirm delete:
Do you really want to delete benchmark?