Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
querySelector() vs getElementsByTagName()[0] prerun
(version: 1)
Comparing performance of:
querySelector vs getElementsByTagName()[0]
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div> <span></span> <span></span> <span></span> <span></span> <span></span> <span></span> <span></span> <span></span> <span></span> <span></span> <span></span> </div>
Script Preparation code:
const div = document.getElementsByTagName('div')[0]; const list = div.getElementsByTagName('span');
Tests:
querySelector
const span = div.querySelector('span');
getElementsByTagName()[0]
const span = list[0]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
querySelector
getElementsByTagName()[0]
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:139.0) Gecko/20100101 Firefox/139.0
Browser/OS:
Firefox 139 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
querySelector
27767724.0 Ops/sec
getElementsByTagName()[0]
48881716.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
In this benchmark, two different methods of selecting elements from the Document Object Model (DOM) are compared: `querySelector()` and `getElementsByTagName()[0]`. Both approaches are utilized in JavaScript to access elements in an HTML document, but they operate differently and have distinct performance characteristics. ### Benchmark Overview 1. **Methods Compared**: - **querySelector**: This method is used to select the first descendant element that matches a specified CSS selector. In this case, it retrieves the first `<span>` element within a given `<div>`. - **getElementsByTagName()[0]**: This method retrieves a live HTMLCollection of elements with the specified tag name. The test accesses the first `<span>` in the collection directly. ### Pros and Cons #### `querySelector()` - **Pros**: - **Flexibility**: Allows for more complex selectors, including class names, IDs, and other CSS selectors. - **Readability**: The code may be easier to read and maintain, particularly for developers familiar with CSS. - **Cons**: - **Performance Overhead**: Depending on the complexity of the selector, this method can be slower as it needs to parse and interpret the selector string. #### `getElementsByTagName()[0]` - **Pros**: - **Performance**: Generally faster for simple tag selection, such as getting the first `<span>`, as it directly accesses the elements without needing to evaluate a selector. - **Live Collection**: The collection returned is live, meaning that it updates when the DOM changes, though this specific test retrieves only the first element. - **Cons**: - **Limited Flexibility**: Can only return elements by tag name; does not support more complex CSS selectors. - **Live Collection Drawbacks**: While it can be an advantage, the live nature may lead to unexpected results if the DOM is manipulated after the collection is created. ### Benchmark Results The benchmark results indicate that `getElementsByTagName()[0]` significantly outperforms `querySelector()` in terms of executions per second: - **getElementsByTagName()[0]**: 20,844,598.0 executions per second - **querySelector**: 6,983,605.5 executions per second This suggests that for straightforward use cases like retrieving the first `<span>` in a known `<div>`, `getElementsByTagName()[0]` is the more efficient choice. ### Other Considerations - **Use Case**: Developers should choose between these methods based on their specific needs. If they require complex selections or are working within a specific CSS context, `querySelector()` is advantageous. However, for simple DOM traversal, especially when performance is critical, `getElementsByTagName()[0]` is recommended. - **Browser Compatibility**: Both methods are widely supported across modern browsers, but it’s worth considering that performance can vary across browser engines. ### Alternatives Other methods and alternatives for element selection include: - **`getElementById()`**: A fast method for selecting a single element by its ID. - **`getElementsByClassName()`**: Similar to `getElementsByTagName`, it retrieves elements by their class name, potentially yielding better performance for accessing multiple elements. - **`querySelectorAll()`**: Returns a static NodeList of all elements that match a CSS selector. It is generally slower than `getElementsByTagName`, particularly when working with a large number of nodes. By considering the pros and cons of each method and understanding the context of their use, developers can make informed choices on which element querying strategy best suits their applications.
Related benchmarks:
querySelectorAll vs getElementsByTagName 2
querySelectorAll vs getElementsByTagName 2z
Get elements by tag and class, vs. only tag, vs. only class
getElementById().getElementsByClassName vs querySelectorAll
querySelectorAll vs getElementsByTagName iteration2
querySelectorAll vs getElementsByTagName iteration 22
querySelectorAll vs getElementsByTagName iteration v2
document getElementsByTagName vs querySelectorAll
querySelector() vs getElementsByTagName()[0]
Comments
Confirm delete:
Do you really want to delete benchmark?