<div class="test" id="test"></div>
document.querySelector(".test")
document.querySelectorAll(".test")
document.getElementsByClassName("test")
document.querySelector("#test")
document.getElementById("test")
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
querySelector | |
querySelectorAll | |
getElementsByClassName | |
querySelector (ID) | |
getElementsByID |
Test name | Executions per second |
---|---|
querySelector | 7697261.0 Ops/sec |
querySelectorAll | 2069892.4 Ops/sec |
getElementsByClassName | 14138496.0 Ops/sec |
querySelector (ID) | 4570806.5 Ops/sec |
getElementsByID | 23114458.0 Ops/sec |
Benchmark Overview
The provided JSON represents a benchmarking test created on MeasureThat.net, which compares the performance of different JavaScript methods for selecting elements in an HTML document.
Methods Being Tested
document.querySelector(".test")
: Uses the querySelector
method with a CSS selector to select an element by its class.document.querySelectorAll(".test")
: Uses the querySelectorAll
method with a CSS selector to select all elements that match the specified class.document.getElementsByClassName("test")
: Uses the getElementsByClassName
method to retrieve a live HTMLCollection of elements whose class attribute contains the specified value.document.querySelector("#test")
: Uses the querySelector
method with an ID selector to select an element by its ID.document.getElementById("test")
: Uses the getElementById
method to retrieve the first element whose id attribute matches the specified value.Comparison of Methods
The pros and cons of each method are:
document.querySelector(".test")
: Fastest, but only selects one element. This is because querySelector
returns a single result, which can lead to less work for the browser engine.document.querySelectorAll(".test")
: Slower than querySelector
, but returns all matching elements. This is because querySelectorAll
returns a NodeList or HTMLCollection of all matched elements.document.getElementsByClassName("test")
: Returns a live HTMLCollection of elements whose class attribute contains the specified value. This is because getElementsByClassName
returns an array-like object that can be iterated over.querySelector
, may require more work for the browser engine due to the live collection naturedocument.querySelector("#test")
: Similar to querySelector
, but uses an ID selector instead of a class selector.document.getElementById("test")
: Returns the first element whose id attribute matches the specified value. This is because getElementById
returns an HTML element or null if no match is found.Library Used
None of the methods require any additional libraries.
Special JS Feature/ Syntax
No special JavaScript features or syntax are used in these tests. The focus is on comparing the performance of different selection methods.
Other Alternatives
If you need to select elements in a more complex scenario, consider using:
document.querySelectorAll("[data-test]")
: Uses an attribute selector to select elements based on their data- attributes.document.querySelector(":is(.test)")
: Uses a CSS pseudo-class to select elements that match a specific class or tag.Keep in mind that the performance differences between these methods may vary depending on the specific use case and browser engine being used.