<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js'></script>
<div id="msglist" data-user="bob" data-list-size="5" data-maxage="180"></div>
var msglist = document.getElementById("msglist");
var show = msglist.getAttribute("data-list-size");
msglist.setAttribute("data-list-size", +show+3);
var msglist = $("#msglist");
var show = msglist.data("list-size");
msglist.data("list-size", +show+3);
var msglist = document.getElementById("msglist");
var show = msglist.dataset.listSize;
msglist.dataset.listSize = +show+3;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
getAttribute and setAttribute | |
jQuery data() method | |
HTML5 dataset API |
Test name | Executions per second |
---|---|
getAttribute and setAttribute | 523681.4 Ops/sec |
jQuery data() method | 352869.2 Ops/sec |
HTML5 dataset API | 428344.8 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark is designed to measure the performance of three different approaches for accessing and modifying HTML5 data attributes in JavaScript:
getAttribute
and setAttribute
(standard DOM method)data()
methodOptions Being Compared
Each test case uses a different approach to access and modify the data-list-size
attribute of an HTML element.
getAttribute
and setAttribute
data()
methoddata()
method to access and modify the attribute value. This method is a convenience wrapper around the standard DOM methods.Pros and Cons of Each Approach
Here's a brief summary of the pros and cons of each approach:
getAttribute
and setAttribute
: This method is widely supported by all modern browsers, but it can be slower than other methods because it involves an extra DOM lookup.data()
method: This method provides a convenient wrapper around the standard DOM methods, but it relies on jQuery being included in the page. Additionally, this method may not be as fast as using the standard DOM methods directly.data()
method.Library Used
In this benchmark, the getAttribute
and setAttribute
methods are used directly in the JavaScript code. The third test case uses jQuery's library to access and modify the attribute value. No other libraries are required besides jQuery for Test Case 2.
Special JS Features or Syntax
None of the test cases use any special JavaScript features or syntax beyond what is standard for modern web browsers. However, it's worth noting that some older versions of Internet Explorer may not support the HTML5 dataset API, which could affect performance in those browsers.
Other Alternatives
If you're interested in optimizing your own code for accessing and modifying data attributes, here are a few alternative approaches:
dataset
property: Instead of using getAttribute
and setAttribute
, you can access data attributes using the dataset
property. For example, var show = msglist.dataset.listSize;
.DOMTokenList
: The HTML5 DOMTokenList API provides a more efficient way to manipulate data attributes by allowing you to set multiple attribute values at once.Note that these alternatives may not provide significant performance improvements unless you're working with large datasets or optimized for specific use cases.