<div>
<ul id="id1">
<li class="c1">Element</li>
<li id="id2" class="c1">Element</li>
<li class="c1">Element</li>
<li class="c1">Element</li>
</ul>
</div>
document.getElementById("id2")
document.querySelector("#id2")
document.getElementsByClassName("c1")[0]
document.querySelector(".c1")
document.getElementById("id1").getElementsByTagName("li")
document.querySelectorAll("#id1 li")
document.getElementById("id1").getElementsByClassName("c1")[0]
document.querySelector("#id1 .c1")
document.getElementsByClassName("c1")
document.querySelectorAll(".c1")
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
getElementById | |
querySelector("#id") | |
getElementsByClassName("class")[0] | |
querySelector(".class") | |
getElementById + getElementsByTagName | |
querySelectorAll("#id tag") | |
getElementById + getElementsByClassName[0] | |
querySelector("#id .class") | |
getElementsByClassName("class") | |
querySelectorAll(".class") |
Test name | Executions per second |
---|---|
getElementById | 2695774.2 Ops/sec |
querySelector("#id") | 1581612.5 Ops/sec |
getElementsByClassName("class")[0] | 2041642.0 Ops/sec |
querySelector(".class") | 2171591.5 Ops/sec |
getElementById + getElementsByTagName | 1937806.2 Ops/sec |
querySelectorAll("#id tag") | 757438.5 Ops/sec |
getElementById + getElementsByClassName[0] | 1752573.8 Ops/sec |
querySelector("#id .class") | 2350441.5 Ops/sec |
getElementsByClassName("class") | 2510279.8 Ops/sec |
querySelectorAll(".class") | 798654.8 Ops/sec |
Measuring JavaScript performance is crucial for understanding the efficiency of different approaches to DOM manipulation.
The provided JSON represents a benchmark test with eight individual test cases, each measuring the performance of different methods for selecting elements in an HTML document. The test cases are:
document.getElementById("id2")
document.querySelector("#id2")
document.getElementsByClassName("c1")[0]
document.querySelector(".c1")
document.getElementById("id1").getElementsByTagName("li")
document.querySelectorAll("#id1 li")
document.getElementById("id1").getElementsByClassName("c1")[0]
document.querySelector("#id1 .c1")
These test cases cover various scenarios, including:
getElementById
)querySelector
or getElementsByClassName
)querySelectorAll
with a tag and class)getElementsByTagName
methodgetElementsByClassName
methodNow, let's analyze the pros and cons of each approach:
1. document.getElementById("id2")
Pros: Simple, reliable, and widely supported. Cons: Can be slow if the element is not found, as it requires a DOM search.
2. document.querySelector("#id2")
Pros: More efficient than getElementById
, as it returns multiple elements if present in the DOM.
Cons: May return unexpected results if the CSS selector is ambiguous or incorrect.
3. document.getElementsByClassName("c1")[0]
Pros: Efficient, as it uses a single method call and returns only one element. Cons: May not work if the class name is not unique (returns all elements with that class).
4. document.querySelector(".c1")
Pros: Similar to getElementsByClassName
, but more concise and readable.
Cons: May return unexpected results if the CSS selector is ambiguous or incorrect.
5. document.getElementById("id1").getElementsByTagName("li")
Pros: Efficient, as it uses a single method call and returns all matching elements in one step. Cons: Returns a HTMLCollection, which may not be as readable as an array of elements.
6. document.querySelectorAll("#id1 li")
Pros: More efficient than getElementsByClassName
, as it returns all matching elements at once.
Cons: May return unexpected results if the CSS selector is ambiguous or incorrect.
7. document.getElementById("id1").getElementsByClassName("c1")[0]
Pros: Similar to getElementsByClassName
, but uses a single method call and returns only one element.
Cons: May not work if the class name is not unique (returns all elements with that class).
8. document.querySelector("#id1 .c1")
Pros: Efficient, as it uses a single method call and returns only one element. Cons: May return unexpected results if the CSS selector is ambiguous or incorrect.
In general, using CSS selectors (querySelector
or getElementsByClassName
) can be more efficient than relying on ID-based selection (getElementById
). However, the performance difference may not be significant in all cases, especially when dealing with complex HTML documents. The best approach ultimately depends on the specific use case and personal preference.