Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
data-attribute vs className 2
(version: 4)
Comparing performance of:
className vs data-attribute
Created:
one year ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div id="foo"></div>
Script Preparation code:
var element = document.createElement('div') element.className='bax' element.setAttribute('baz', '1')
Tests:
className
console.log(element.className.slice(1,2))
data-attribute
console.log(element.dataset.baz)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
className
data-attribute
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/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
className
463885.8 Ops/sec
data-attribute
440963.8 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark you're looking at compares two different approaches for accessing data associated with an HTML element in JavaScript: using `className` and `data-attributes`. Specifically, it contrasts: 1. **Accessing `className`**: This approach uses the `className` property of an HTML element. The test evaluates the performance of retrieving a portion of this property using the `slice` method on a string. In this case, it targets characters within the string, simulating typical usage of class names in scripts. - **Pros**: - Directly uses standard properties which are widely supported and understood. - Can be more performant in certain cases since accessing properties like `className` is typically straightforward and may have fewer overheads compared to custom attributes. - **Cons**: - The class attribute is often used for styling purposes and can result in name collisions if multiple classes are used. This approach may lack semantic clarity when used to store arbitrary data. 2. **Using `data-attributes`**: This approach leverages the HTML5 `data-*` attributes, which are specifically designed to store custom data directly on elements. The test accesses this data using the `dataset` property. - **Pros**: - Provides clear semantics for data storage, allowing developers to understand the purpose of the data related to the element directly in HTML. - Also makes the markup more readable and maintainable. The data attributes are validated in HTML, providing another layer of robustness. - **Cons**: - Can introduce slightly more overhead than simple property accesses since it involves parsing and potentially additional operations internally when utilizing the `dataset`. - Might be less performant in scenarios with extensive DOM manipulation or access patterns compared to simpler property access methods. ### Additional Considerations: - **Library Use**: The benchmark does not involve any external JavaScript libraries; it only uses native JS methods. The `dataset` property is a built-in feature of the HTML DOM API. - **Test Conditions**: The benchmark results show performance in terms of "Executions Per Second" for each method across a specific browser environment (Chrome 131 on macOS). In this case, `className` had a slightly faster execution rate compared to `data-attributes`, indicating that accessing the `className` property was marginally more efficient. ### Alternatives: Other methodologies for handling similar data could include: - **Using JSON Objects**: Instead of embedding data in attributes, you can use a dedicated JavaScript object to store related element data. This can simplify access patterns especially for complex data structures. - **Custom Libraries**: Libraries like jQuery can abstract away differences in accessing and manipulating DOM data, providing a unified method to deal with classes and attributes. - **Inline Styles or Other Attributes**: Depending on the use case, data could also be stored in inline styles or other attributes, although this is typically less advisable for maintaining semantic separation of concerns. Overall, choosing between `className` and `data-attributes` typically depends on the specific requirements of your application, considering factors like performance, maintainability, and clarity of code.
Related benchmarks:
className vs. setAttribute vs. classList
getAttribute/setAttribute versus classList
Set ID
setAttribute vs classList.add (attribute adding)
setAttribute vs classList.add (attribute adding) V2
className + attribute vs attribute + className
getAttribute('data-foo') vs dataset.foo
id vs getAttribute pt 2
setAttribute vs classList.add
Comments
Confirm delete:
Do you really want to delete benchmark?