Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
getElementById vs querySelector2
(version: 0)
Comparing performance of:
getElementById vs querySelector
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="testElement2">You can't do it with the strike tag OR the text-decoration:line-through style. The line position is built-in. You could roll your own style for that, but it would be a huge PITA.</div> <div id="testElement12"></div> <div id="testElement"></div> <div> <div id="testElement4"><button></button></div> <div id="testElement5"></div> </div> <div id="testElement6"></div> <div id="testElement7">You can't do it with the strike tag OR the text-decoration:line-through style. The line position is built-in. You could roll your own style for that, but it would be a huge PITA.</div> <div id="testElement8"></div> <div id="testElement9"></div>
Tests:
getElementById
var el = document.getElementById('testElement'); var className = el.className;
querySelector
var el = document.querySelector('#testElement'); var className = el.className;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
getElementById
querySelector
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0
Browser/OS:
Firefox 122 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
getElementById
9237728.0 Ops/sec
querySelector
4322403.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark measures the performance difference between two JavaScript methods: `document.getElementById` and `document.querySelector`. The test compares these two approaches to retrieve an element's class name. **What is tested?** * `document.getElementById`: A traditional method to select an element by its ID. * `document.querySelector`: A more modern method to select elements based on a CSS selector or attribute. **Options compared** The benchmark compares the performance of `getElementById` and `querySelector`. These methods differ in their approach: * `getElementById`: Directly searches for an element by its ID, which is typically unique. * `querySelector`: Searches for an element that matches a given CSS selector or attribute. This method returns multiple elements if there are multiple matches. **Pros and Cons** * `getElementById`: + Pros: Fast, reliable, and widely supported. + Cons: Can be slow if the ID is not unique (e.g., when used with multiple elements having the same ID). * `querySelector`: + Pros: Flexible and powerful for complex selectors or attributes. + Cons: May return multiple matches, which can lead to slower performance. **Library and purpose** There are no libraries mentioned in this benchmark. Both `document.getElementById` and `document.querySelector` are built-in methods of the Document Object Model (DOM) in JavaScript. **Special JS features or syntax** No special JavaScript features or syntax are used in these test cases. The focus is solely on comparing the performance of two DOM-related methods. **Other alternatives** If you're interested in exploring alternative approaches, consider: * `document.getElementsByTagName`: Retrieves a list of all elements matching a given tag name. * `document.querySelectorAll`: A more powerful version of `querySelector` that returns all matches from a single selector string. Keep in mind that these alternatives might have different performance characteristics compared to the original methods tested in this benchmark.
Related benchmarks:
Set style vs setAtrribute("style") 3
CssText vs Styling
CssText vs Styling vs classList add
getElementById vs querySelector, with ten elements
Comments
Confirm delete:
Do you really want to delete benchmark?