<div id="testdiv06">
<div id="unique06" class="unique" name="unique" data-unique="1">test</div>
</div>
<div id="testdiv05">
<div id="unique05" class="unique" name="unique" data-unique="1">test</div>
</div>
<div id="testdiv04">
<div id="unique04" class="unique" name="unique" data-unique="1">test</div>
</div>
<div id="testdiv03">
<div id="unique03" class="unique" name="unique" data-unique="1">test</div>
</div>
<div id="testdiv02">
<div id="unique02" class="unique" name="unique" data-unique="1">test</div>
</div>
<div id="testdiv01">
<div id="unique01" class="unique" name="unique" data-unique="1">test</div>
</div>
<div id="testdiv">
<div id="unique" class="unique" name="unique" data-unique="1">test</div>
</div>
var i, imax;
var doc = document;
var formelem = doc.getElementById('testdiv').getElementsByTagName('div')[0].innerHTML;
var test = doc.getElementById('testdiv').querySelector('.unique').innerHTML;
var test = doc.getElementById('testdiv').getElementsByClassName('unique')[0].innerHTML;
var test = doc.getElementsByName('unique')[0].innerHTML;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
getElementsByTagName | |
querySelector | |
getElementsByClassName | |
getElementsByName |
Test name | Executions per second |
---|---|
getElementsByTagName | 1193788.9 Ops/sec |
querySelector | 1406784.0 Ops/sec |
getElementsByClassName | 1209756.8 Ops/sec |
getElementsByName | 1387566.6 Ops/sec |
Benchmark Overview
The provided benchmark tests the performance of different DOM element retrieval methods in JavaScript: getElementById
, getElementsByTagName
, querySelector
, getElementsByClassName
, and getElementsByName
. The test case uses HTML elements with unique IDs, classes, and names to simulate a real-world scenario.
Options Compared
getElementById
: This method retrieves an element by its ID.getElementsByTagName
: This method retrieves all elements of a specific tag name.querySelector
: This method retrieves the first element that matches a CSS selector.getElementsByClassName
: This method retrieves all elements with a specific class name.getElementsByName
: This method is not a standard JavaScript method, and it's unclear what this option is supposed to do. It may be a typo or a non-standard implementation.Pros and Cons of Each Approach
getElementById
:getElementsByTagName
:getElementById
.querySelector
:getElementsByClassName
:getElementById
.Library and Purpose
In this benchmark, no libraries are explicitly mentioned. However, it's likely that the test case uses the built-in JavaScript DOM API to interact with the HTML elements.
Special JS Features or Syntax
There doesn't seem to be any special JavaScript features or syntax used in this benchmark, apart from possibly using non-standard methods like getElementsByName
.
Other Alternatives
Some alternative approaches for retrieving DOM elements include:
querySelectorAll
(a combination of querySelector
and textContent
) to retrieve all matching elements.Document.querySelector()
or Document.querySelectorAll()
with a CSS selector to retrieve elements based on class names, IDs, or attributes.find()
method to retrieve elements.It's worth noting that the performance of these alternatives may vary depending on the specific use case and browser implementation.