Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
document.querySelectorAll vs document.body.querySelectorAll
(version: 0)
Comparing performance of:
querySelectorAll vs getElementsByClassName
Created:
7 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div class="test"></div>
Tests:
querySelectorAll
document.querySelectorAll(".test")
getElementsByClassName
document.body.querySelectorAll(".test")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
querySelectorAll
getElementsByClassName
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/28.0 Chrome/130.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 130 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
querySelectorAll
4209719.0 Ops/sec
getElementsByClassName
4372536.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's dive into the details of this benchmark. **Benchmark Description** The benchmark compares two different ways to select HTML elements on a webpage: 1. `document.querySelectorAll` (short for querySelectorAll) - a method that returns all elements matching a specified CSS selector, in this case `.test`. 2. `document.body.querySelectorAll` - an alternative approach that selects the body element first and then uses its own `querySelectorAll` method to select elements with class `.test`. **What is being tested?** In this benchmark, we're measuring how quickly each of these two approaches can execute on a webpage. **Test Case Options** The test cases compare the execution speed of: 1. **`document.querySelectorAll(".test")`**: This method selects all elements with the class `".test"` directly from the document. 2. **`document.body.querySelectorAll(".test")`**: This approach first gets the body element using `document.body`, and then uses its own `querySelectorAll` method to select elements with class `".test"`. It's essentially a nested call, where we're querying the body element first. **Pros/Cons of Different Approaches** Here are some considerations for each approach: 1. **`document.querySelectorAll(".test")`**: Pros: * Direct and efficient way to select elements. * Avoids unnecessary nesting (i.e., calling `querySelectorAll` on the body element). * Generally faster, as shown in the benchmark results. Cons: * May not be suitable if you need to operate on a specific scope (e.g., only selecting elements within the body or a specific container). 2. **`document.body.querySelectorAll(".test")`**: Pros: * Allows for more targeted selection within the scope of the body element. * Can be useful when working with complex HTML structures. Cons: * May introduce additional overhead due to the nested call. **Library and Special JS Features** In this benchmark, there is no specific library or feature used. The code relies on standard JavaScript methods (`document.querySelectorAll` and `document.body.querySelectorAll`) available in most modern browsers. **Other Alternatives** For selecting elements on a webpage, you can also use: 1. **`getElementsByClassName`**: This method returns an HTMLCollection of elements with the specified class name. 2. **CSS Selectors**: You can use CSS selectors directly within your JavaScript code to select elements (e.g., `document.querySelector(".test")` or `document.querySelectorAll(".test")`). 3. **Other DOM methods**: Depending on your specific use case, you might find other DOM methods more suitable for selecting elements. Overall, the benchmark demonstrates a simple and efficient approach using `document.querySelectorAll`. However, it's essential to consider the context of your project when choosing between different selection methods.
Related benchmarks:
document.querySelectorAll vs document.body.querySelectorAll
getElementsByClassName()[0] vs querySelectorAll
document.body vs document.querySelector
JS getElementsByClassName vs querySelectorAll
Comments
Confirm delete:
Do you really want to delete benchmark?