<div class="hello" id="hello" data-hello>Hello</div>
document.querySelector('.hello');
document.querySelector('#hello');
document.querySelector('[data-hello]');
document.querySelector('.hello[data-hello]');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Class | |
ID | |
data attribute | |
Class attribute |
Test name | Executions per second |
---|---|
Class | 7059036.0 Ops/sec |
ID | 5012498.5 Ops/sec |
data attribute | 4885156.0 Ops/sec |
Class attribute | 5766518.0 Ops/sec |
The benchmark provided compares the performance of various CSS selectors in the document.querySelector
method of JavaScript. The tests evaluate how efficiently different types of selectors can retrieve an HTML element from the DOM, specifically targeting an element set up with a class, an ID, and a data attribute.
Class Selector:
document.querySelector('.hello');
ID Selector:
document.querySelector('#hello');
Data Attribute Selector:
document.querySelector('[data-hello]');
data-*
HTML5 syntax). This is useful for embedding extra information on elements without using custom attributes.Class & Data Attribute Selector:
document.querySelector('.hello[data-hello]');
The benchmark results indicate the number of executions per second for each selector type on a specific browser and platform.
Class Selector: 7,059,036 executions/second - The fastest among the tests, likely due to the optimization of class selectors in modern JavaScript engines.
Class & Data Attribute Selector: 5,766,518 executions/second - Also relatively fast, but slightly slower than just a class selector, which is expected since it requires checking two conditions.
ID Selector: 5,012,498.5 executions/second - Slower than the class selector but still efficient. The uniqueness of ID selectors typically allows for faster retrieval, although it might not have as much performance boost as expected due to the added complexity of the browser's selector engine.
Data Attribute Selector: 4,885,156 executions/second - This selector type shows the slowest performance in this benchmark. While useful for specific use cases, data attribute selectors tend to aggregate less optimized retrieval times compared to class and ID selectors.
Class Selector:
ID Selector:
Data Attribute Selector:
Class & Data Attribute Selector:
Performance Optimization: While data attributes can be slower, their flexibility often compensates for this in real-world applications where semantic data is critical. Developers should balance performance needs with the necessity of using more informative attributes.
Alternative Libraries: Libraries such as jQuery provide high-level abstractions that can simplify the use of selectors across different browsers, enabling consistent behavior. However, using jQuery can introduce performance overhead due to its additional abstraction layer.
Native DOM Manipulation: Direct use of native JavaScript (document.querySelector
or document.getElementById
) is optimal for speed-sensitive applications. This minimizes overhead and enhances performance.
CSS Selectors vs. XPath: In scenarios where complex queries are necessary, XPath provides more powerful querying capabilities, especially in XML documents and when dealing with nested structures. However, it is not natively supported in browsers for HTML documents.
Caching Selectors: For performance-critical applications, it’s advisable to cache selected elements after the first execution, reducing redundant queries on the same selectors.
In summary, this benchmark provides valuable insights into the performance landscape of various CSS selectors in JavaScript, enabling developers to make informed decisions based on their specific requirements and constraints.