Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Body selector speed
(version: 3)
Comparing performance of:
document.body vs getElementsByTagName vs querySelector
Created:
5 years ago
by:
Registered User
Jump to the latest result
Tests:
document.body
if(document.body.classList.contains('webp-on')){console.log(!0)}else{console.log(!1)} if(document.body.classList.contains('webp-on')){console.log(!0)}else{console.log(!1)} if(document.body.classList.contains('webp-on')){console.log(!0)}else{console.log(!1)}
getElementsByTagName
if(document.getElementsByTagName('body')[0].classList.contains('webp-on')){console.log(!0)}else{console.log(!1)} if(document.getElementsByTagName('body')[0].classList.contains('webp-on')){console.log(!0)}else{console.log(!1)} if(document.getElementsByTagName('body')[0].classList.contains('webp-on')){console.log(!0)}else{console.log(!1)}
querySelector
if(document.querySelector('body').classList.contains('webp-on')){console.log(!0)}else{console.log(!1)} if(document.querySelector('body').classList.contains('webp-on')){console.log(!0)}else{console.log(!1)} if(document.querySelector('body').classList.contains('webp-on')){console.log(!0)}else{console.log(!1)}
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
document.body
getElementsByTagName
querySelector
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):
**Benchmark Overview** The provided benchmark measures the speed of three different approaches for checking if an HTML element exists and has a specific class. **Approaches Compared** 1. **`document.body.classList.contains('webp-on')`**: This approach uses the `classList` property to check if the `body` element has a specific class (`'webp-on'`). If it does, the code executes the `console.log(!0)` statement; otherwise, it logs `console.log(!1)`. 2. **`document.getElementsByTagName('body')[0].classList.contains('webp-on')`**: This approach uses the `getElementsByTagName` method to retrieve all elements with the tag name `'body'`, selects the first one (assuming there's only one), and then checks if its class list contains `'webp-on'`. 3. **`document.querySelector('body').classList.contains('webp-on')`**: This approach uses the `querySelector` method to select an element with the tag name `'body'` using a CSS selector. It then checks if the selected element's class list contains `'webp-on'`. **Pros and Cons of Each Approach** 1. **`document.body.classList.contains('webp-on')`**: * Pros: Fast, lightweight, and widely supported. * Cons: May not work correctly in older browsers or when used with other elements that have the same class name. 2. **`document.getElementsByTagName('body')[0].classList.contains('webp-on')`**: * Pros: Works with all HTML versions, but is slightly slower due to the `getElementsByTagName` method's overhead. * Cons: May fail if there are multiple elements with the tag name `'body'`. 3. **`document.querySelector('body').classList.contains('webp-on')`**: * Pros: Fast and flexible, as it allows using CSS selectors for more complex queries. * Cons: May be slower than `document.body.classList.contains('webp-on')`, especially in older browsers. **Library Usage** There is no explicit library usage mentioned in the benchmark. However, some browser-specific APIs may be used indirectly (e.g., Chrome's `querySelectorAll` method uses a library-like API under the hood). **Special JavaScript Features or Syntax** None are explicitly mentioned. **Other Considerations** * The benchmark measures the speed of these approaches alone, without considering other factors like memory usage or side effects on the DOM. * It's essential to note that this benchmark may not accurately represent real-world scenarios, as it only tests a specific snippet of code in isolation. **Alternatives** If you need more comprehensive benchmarking or want to compare different approaches for checking HTML element existence and class presence, consider using alternative libraries or tools like: * Benchmarking frameworks like `benchmark.js` or `fast-bench` * Web performance measurement tools like `webpagetest` or `Lighthouse` * Browser-specific performance APIs (e.g., Chrome's `PerformanceNavigation.getEntries()` method) Keep in mind that the choice of tool depends on your specific use case and requirements.
Related benchmarks:
jQuery 2.1.4 selector vs document.querySelector
jQuery 3.3.1 selector vs document.querySelector
jQuery 3.3.1 selector vs document.querySelectorAll
document.querySelector vs document.body.querySelector
document.body vs document.querySelector
Comments
Confirm delete:
Do you really want to delete benchmark?