<div class="hello" id="hello" data="hello">Hello</div>
document.querySelector('.hello');
document.querySelector('#hello');
document.querySelector('[data="hello"]');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Class | |
ID | |
Attribute |
Test name | Executions per second |
---|---|
Class | 18200528.0 Ops/sec |
ID | 8714885.0 Ops/sec |
Attribute | 13484226.0 Ops/sec |
I'll break down the provided JSON and explain what's being tested, the different approaches compared, their pros and cons, and other considerations.
Benchmark Definition
The benchmark definition is a simple JavaScript code snippet that uses the document.querySelector()
method to select an HTML element based on its class, ID, or attribute value. The goal is to compare the performance of these three approaches:
document.querySelector('.hello');
document.querySelector('#hello');
document.querySelector('[data="hello"]');
Options Compared
The benchmark compares the execution time of each approach on different browsers and devices.
Pros and Cons of Different Approaches:
id="hello-123"
).[data="hello"]
).Library Used
In this benchmark, no libraries are explicitly mentioned. However, document.querySelector()
is a part of the DOM (Document Object Model) API, which is a built-in JavaScript API for working with HTML and XML documents.
Special JS Features or Syntax
None mentioned.
Other Considerations
When comparing these approaches, it's essential to consider the following:
Alternatives
If you want to test similar approaches, consider using benchmarks like:
element-from-viewport
(for selecting elements based on their position in the viewport)css-selector-performance
(for comparing CSS selector performance)Keep in mind that these alternatives might have slightly different use cases or requirements. MeasureThat.net's benchmark is well-suited for testing class selectors, ID selectors, and attribute selectors in a general-purpose scenario.