Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
querySelectorAll vs. getElementsByTagName
(version: 0)
Comparing performance of:
querySelectorAll vs getElementsByTagName
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<table id="data-table"> <tr> <th colspan="6">Results</th> </tr> <tr> <td>No</td> <td>Competition</td> <td>John</td> <td>Adam</td> <td>Robert</td> <td>Paul</td> </tr> <tr> <td>1</td> <td>Swimming</td> <td>1:30</td> <td>2:05</td> <td>1:15</td> <td>1:41</td> </tr> <tr> <td>2</td> <td>Running</td> <td>15:30</td> <td>14:10</td> <td>15:45</td> <td>16:00</td> </tr> <tr> <td>3</td> <td>Shooting</td> <td>70%</td> <td>55%</td> <td>90%</td> <td>88%</td> </tr> </table>
Script Preparation code:
var dataTable = document.getElementById('data-table');
Tests:
querySelectorAll
dataTable.querySelectorAll("tr")
getElementsByTagName
dataTable.getElementsByTagName('TR')
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/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
querySelectorAll
3015292.8 Ops/sec
getElementsByTagName
33338854.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript benchmark that compares the performance of two DOM manipulation methods: `querySelectorAll` and `getElementsByTagName`. The benchmark is designed to test how quickly these methods can traverse an HTML table element (`dataTable`) and retrieve all its descendant elements. **Methods Compared** The benchmark tests two methods: 1. **`querySelectorAll`**: This method returns a NodeList containing all elements that match the specified CSS selector. 2. **`getElementsByTagName`**: This method returns a collection of elements with the specified tag name. **Pros and Cons of Each Approach** ### `querySelectorAll` Pros: * More expressive and flexible than `getElementsByTagName`, allowing for more complex selectors. * Can be faster than `getElementsByTagName` because it uses a single operation to retrieve all matching elements, whereas `getElementsByTagName` requires multiple calls to iterate over the table rows. Cons: * May have slower performance in older browsers or with very large datasets due to its flexibility and the need to parse CSS selectors. ### `getElementsByTagName` Pros: * Faster than `querySelectorAll` for simple cases, as it only needs to find a single element. * More reliable and robust in older browsers that don't support CSS selectors. Cons: * Less expressive and less flexible than `querySelectorAll`, requiring multiple calls to retrieve all elements. * May be slower for complex selections or large datasets due to the need for iterative searches. **Library Usage** The benchmark uses the `document` object, which is a built-in JavaScript library that provides access to HTML documents. The `document.getElementById` method is used to select an element by its ID, in this case, `dataTable`. **Special JS Feature or Syntax** There are no special JavaScript features or syntaxes used in this benchmark. **Other Considerations** The benchmark assumes that the input HTML structure is a table with rows and columns. If the table structure changes, the benchmark may need to be updated to accommodate the new structure. **Alternatives** Other alternatives for comparing these methods could include: * Using `querySelector` instead of `querySelectorAll`, which would test only the first matching element. * Using `getElementsByClassName` or `getElementsByTagNameNS` to test other DOM selection methods. * Creating a more complex HTML structure with multiple tables, rows, and columns to simulate real-world scenarios. Overall, this benchmark provides a useful comparison of two widely used JavaScript methods for DOM manipulation.
Related benchmarks:
getElementsByClassName vs querySelectorAll
querySelector vs getElementsByClassName[0] with real dom
Move Multiple Elements to another - Winners
createContextualFragment vs template.innerHTML
Comments
Confirm delete:
Do you really want to delete benchmark?