Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
DOM selector benchmark about class, data attribute
(version: 1)
Comparing performance of:
class vs data attribute
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div class="bar" data-value="true"></div> <div class="bar" data-value="true"></div> <div class="bar" data-value="true"></div> <div class="bar" data-value="true"></div> <div class="bar" data-value="false"></div> <div class="bar" data-value="true"></div> <div class="bar" data-value="false"></div> <div class="bar" data-value="false"></div> <div class="bar" data-value="true"></div> <div class="bar" data-value="true"></div>
Tests:
class
const $selector = document.querySelectorAll('.bar'); $selector.forEach(element => { if (JSON.parse(element.dataset.value)) { console.log(element.dataset.value); } });
data attribute
const $selector = document.querySelectorAll('[data-value="true"]'); $selector.forEach(element => { console.log(element.dataset.value); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
class
data attribute
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):
Let's break down the provided benchmark and explain what is tested, the different approaches compared, their pros and cons, and other considerations. **Benchmark Definition** The provided benchmark definition represents a JavaScript microbenchmark that tests two approaches: using CSS class selectors and using data attributes to filter DOM elements. The benchmark aims to measure which approach is faster for this specific use case. **Options Compared** There are two test cases: 1. **Class**: This approach uses the `document.querySelectorAll('.bar')` method to select all elements with the class "bar", and then iterates through the resulting NodeList using a `forEach` loop to log the value of the `data-value` attribute. 2. **Data Attribute**: This approach uses the `[data-value="true"]` pseudo-class to select all elements that have a `data-value` attribute set to `"true"`, and then iterates through the resulting NodeList using a `forEach` loop to log the value of the `data-value` attribute. **Pros and Cons** * **Class Approach**: Pros: + More widely supported by older browsers. + Can be used for more complex selection scenarios (e.g., multiple classes). Cons: + May be slower due to the need to use the `querySelectorAll()` method, which can be slower than using a pseudo-class. * **Data Attribute Approach**: Pros: + Faster, as it uses a pseudo-class that is optimized by modern browsers for performance. + More concise and easier to read. Cons: + Requires the `data-value` attribute to be set on each element, which may not always be possible or desirable. **Library** None of the provided benchmark code uses any external libraries. **Special JS Feature or Syntax** The `forEach` loop is a standard JavaScript feature. However, it's worth noting that some older browsers (e.g., Internet Explorer 9) had issues with `forEach`, which led to polyfills being created for this purpose. **Other Considerations** * **Browser Support**: The benchmark assumes that the browser supports modern CSS pseudo-classes and the `data` attribute. Older browsers may not support these features, which could affect the results. * **Optimization**: Some browsers (e.g., Chrome) have optimization techniques in place to improve performance for specific use cases, such as the `classList` API or native web worker support. **Alternatives** If you were to recreate this benchmark, you might consider adding additional test cases to cover other approaches, such as: * Using a library like Lodash or jQuery to perform the filtering. * Using a different data structure, such as an array or Set, instead of a NodeList. * Adding more complex selection scenarios or filtering conditions. Keep in mind that optimizing performance-critical code requires thorough testing and analysis, taking into account various browser differences and optimizations.
Related benchmarks:
jQuery vs Vanilla JS Selectors with larger DOM
querySelectorAll data attribute without value consideration vs class name
querySelectorAll data attribute (without value) vs class name
className vs data attr querySelectorAll vs getElementsByClassName
Comments
Confirm delete:
Do you really want to delete benchmark?