Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
querySelectorAll vs getElementsByTagName iteration 222
(version: 0)
Comparing performance of:
querySelectorAll vs getElementsByTagName
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div>
Tests:
querySelectorAll
const allDivs = document.querySelectorAll('div'); for(const d of allDivs){}
getElementsByTagName
const allDivs = document.getElementsByTagName('div'); for(const d of allDivs){}
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
querySelectorAll
getElementsByTagName
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
querySelectorAll
790321.3 Ops/sec
getElementsByTagName
1004439.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark compares two ways to retrieve all `<div>` elements from an HTML document: `document.querySelectorAll('div')` (querySelectorAll) vs `document.getElementsByTagName('div')` (getElementsByTagName). **What are we comparing?** We are comparing the performance of two approaches: 1. **querySelectorAll**: This method uses a single query and returns a NodeList containing all matching elements. 2. **getElementsByTagName**: This method uses a separate call for each element type and returns an HTMLCollection containing all matching elements. **Pros and Cons** **querySelectorAll:** Pros: * More efficient, as it uses a single query and can return all matching elements in a single step. * Less code to write, as it eliminates the need for multiple `getElementsByTagName` calls. Cons: * May not be supported by older browsers or those with broken DOM implementations. * Can be slower if the document is very large, since it returns a large NodeList that needs to be iterated over. **getElementsByTagName:** Pros: * More widely supported, as it's been around for longer and is often implemented more robustly in older browsers. * Can be faster for very large documents, since each element is retrieved individually. Cons: * Requires multiple calls for each element type, which can increase code complexity and overhead. * Slower than `querySelectorAll` in many cases, due to the additional overhead of individual element retrieval. **Other Considerations** In modern browsers, both approaches are generally fast and efficient. However, the actual performance difference between them may depend on factors like: * Document size and complexity * Browser version and capabilities * CPU architecture and cache efficiency **Library Usage** Neither `querySelectorAll` nor `getElementsByTagName` rely on external libraries. They are built-in DOM methods that work directly with HTML documents. **Special JavaScript Features or Syntax** There is no mention of special JavaScript features or syntax in the benchmark. Both approaches use standard DOM methods and do not require any additional library or framework-specific code. **Alternatives** If you need to compare performance between other DOM retrieval methods, some alternatives include: * `document.querySelector('[type="div"]')` (a more specific query that can be faster for smaller documents) * `querySelectorAll('[data-div]')` (a query that uses data attributes instead of element types) * Using a library like Lodash or Underscore.js to abstract away DOM complexity Keep in mind that the actual performance differences between these alternatives may vary depending on the specific use case and browser version.
Related benchmarks:
querySelector() vs getElementsByClassName()[0]
querySelector vs getElementsByClassName[0] Perf
getElementsByClassName()[0] vs querySelectorAll
querySelector vs getElementsByTagName
querySelector vs getElementByTagName
Comments
Confirm delete:
Do you really want to delete benchmark?