最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - What is the fastest way to get multiple data-attributes from the same element - Stack Overflow

matteradmin8PV0评论

I have a use case which requires getting multiple html5 data-attributes from elements. This occurs within a loop across many elements and pushes the data in to an array for caching.

From doing some research it appears that using el.getAttribute('data-whatever') is faster than using the native html5 dataset method however I need to get around 5 separate data attributes from the same element.

The dataset method allows you to retrieve all data-attributes with one call and then access them with standard object dot notation ( dataset.index, dataset.whatever, dataset.somethingelse ) whereas using getAttribute I have to make repeated getAttribute calls to retrieve all the needed data.

If using non pliant attributes I can of course simply use dot notation to access the properties speeding up this function considerably. But using html5 data attributes this does not work (i.e. el.data-whatever will always be undefined).

I wish to remain pliant to standards therefore I am looking for the fastest possible way of retrieving these multiple data attributes.

Thanks in advance.

I have a use case which requires getting multiple html5 data-attributes from elements. This occurs within a loop across many elements and pushes the data in to an array for caching.

From doing some research it appears that using el.getAttribute('data-whatever') is faster than using the native html5 dataset method however I need to get around 5 separate data attributes from the same element.

The dataset method allows you to retrieve all data-attributes with one call and then access them with standard object dot notation ( dataset.index, dataset.whatever, dataset.somethingelse ) whereas using getAttribute I have to make repeated getAttribute calls to retrieve all the needed data.

If using non pliant attributes I can of course simply use dot notation to access the properties speeding up this function considerably. But using html5 data attributes this does not work (i.e. el.data-whatever will always be undefined).

I wish to remain pliant to standards therefore I am looking for the fastest possible way of retrieving these multiple data attributes.

Thanks in advance.

Share Improve this question asked May 23, 2013 at 16:33 gordyrgordyr 6,29614 gold badges67 silver badges123 bronze badges 3
  • 2 You can make tests at jsperf. – Andreas Louv Commented May 23, 2013 at 16:35
  • 1 It seems unlikely that fetching attributes from DOM nodes would be the focal point of performance. Have you profiled your code with Firefox or Chrome (or anything else) to look for "hot spots"? – Pointy Commented May 23, 2013 at 16:42
  • @Pointy Yes I have profiled in detail. This is an extremely unusual use case and as such this kind of optimization is actual warranted. Thanks anyway :) – gordyr Commented May 23, 2013 at 16:49
Add a ment  | 

2 Answers 2

Reset to default 5

I made this test:

http://jsperf./dataset-vs-getattribute-2

The test is a the following:

dataset all:

var data = el.dataset;

getAttribute all:

var data = {
    id: el.getAttribute('data-id'),
    user: el.getAttribute('data-user'),
    dateOfBirth: el.getAttribute('data-date-of-birth')
};

dataset single:

var user = el.dataset.user;

getAttribute single:

var user = el.getAttribute('user');

https://developer.mozilla/en-US/docs/Web/API/element.dataset

Just benchmark it :

http://jsperf./dataset-access-vs-getattrb

It still looks to be 2 times faster using getAttribute on chrome.

I'm using a noop to prevent the JIT from optimizing it away.

Post a comment

comment list (0)

  1. No comments so far