Performance: Data attributes
Using data attributes affects DOM size and JavaScript access speed, impacting interaction responsiveness.
<div data-info='{"info1":"value1","info2":"value2","info3":"value3"}'></div>
<div data-info1="value1" data-info2="value2" data-info3="value3" data-info4="value4" data-info5="value5"></div>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Many separate data attributes | Increases DOM size | No direct reflows but slows JS | Minimal paint impact | [X] Bad |
| Single JSON data attribute | Smaller DOM size | No reflows | Minimal paint impact | [OK] Good |
data- attributes in HTML?<div> element?<button data-action="save" data-id="42">Save</button>
button.dataset.action return in JavaScript?<span data-user_id="7">User</span>
<section> element using data attributes. Which of these is the best and valid way to do it?Pass the test and this pattern moves unknown