Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
getElementById and getElementsByClassName [0] vs querySelector
(version: 0)
Test performance of different ways of get just one particular DOM element
Comparing performance of:
getElementById vs querySelector vs getElementById and getElementsByClassName
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="testdiv"> <div id="unique1" class="unique" name="unique" data-unique="1">test1</div> <div id="unique2" class="unique" name="unique" data-unique="2">test2</div> <div id="unique3" class="unique" name="unique" data-unique="3">test3</div> <div id="unique4" class="unique" name="unique" data-unique="4">test4</div> <div id="unique5" class="unique" name="unique" data-unique="5">test5</div> </div>
Script Preparation code:
var i, imax; var doc = document;
Tests:
getElementById
var test = doc.getElementById('testdiv').childNodes[0].innerHTML;
querySelector
var test = doc.querySelector('#testdiv .unique').innerHTML;
getElementById and getElementsByClassName
var test = doc.getElementById('testdiv').getElementsByClassName('unique')[0].innerHTML;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
getElementById
querySelector
getElementById and getElementsByClassName
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/123.0.0.0 Safari/537.36 OPR/109.0.0.0
Browser/OS:
Opera 109 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
getElementById
4348930.0 Ops/sec
querySelector
1997115.4 Ops/sec
getElementById and getElementsByClassName
2193855.2 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. **Benchmark Overview** The test measures the performance of three different approaches to retrieve the inner HTML of a specific DOM element: 1. `getElementById` 2. `getElementById` with `getElementsByClassName` (combined) 3. `querySelector` These tests are designed to measure the speed difference between these three approaches in retrieving a single, unique DOM element. **Options Compared** * `getElementById`: uses the `document.getElementById()` method to retrieve an element by its ID. * `getElementById and getElementsByClassName`: combines `document.getElementById()` with `getElementsByClassName()` to retrieve an element by both its ID and class name. * `querySelector`: uses the `document.querySelector()` method to select an element based on a CSS selector. **Pros and Cons of Each Approach** * `getElementById`: + Pros: Simple, efficient, and widely supported. + Cons: Can be slow if the element is deeply nested or has many siblings. * `getElementById and getElementsByClassName`: + Pros: Allows retrieving elements by multiple criteria (ID and class name). + Cons: Can be slower than using `getElementById` alone due to the additional method call and array iteration. * `querySelector`: + Pros: Flexible, efficient, and widely supported. Can select elements based on complex CSS selectors. + Cons: May be overkill for simple cases where only a single element is needed. **Library Used** None explicitly mentioned in the provided code. However, it's likely that the tests are using built-in JavaScript libraries or DOM APIs to interact with the HTML document. **Special JS Feature/Syntax** There doesn't seem to be any special JS feature or syntax used in this benchmark (e.g., ES6+ features like async/await, arrow functions). The code appears to use traditional JavaScript methods and syntax. **Other Alternatives** If you need to retrieve an element by ID or class name, other approaches might include: * `querySelectorAll()` for retrieving multiple elements that match a CSS selector * Using a library like jQuery for its DOM manipulation capabilities * Utilizing other libraries like React or Angular for their component-based approach Keep in mind that this benchmark is focused on measuring the performance of specific methods, not exploring alternative approaches. If you have any further questions or would like more information on these topics, feel free to ask!
Related benchmarks:
Test performance of different ways of get just one particular DOM element
getElementById vs querySelector vs getElementsByClassName
getElementById vs querySelector vs getElementsByClassName vs getElementsByName 2
getElementById vs getElementsByClassName V2
Comments
Confirm delete:
Do you really want to delete benchmark?