<form id="testform">
<input value="test" />
<input value="testing" />
</form>
<div id="testdiv">
<input class="unique" name="unique" data-unique="1" value="test" />
<input class="unique" name="unique" data-unique="1" value="testing" />
<!-- added following divs to try and knock getElementsByClassName and getElementsByName out of the running-->
<div class="test1" name="test1">
<div class="test6" name="test6"> </div>
</div>
<div class="test2" name="test2">
<div class="test7" name="test7"> </div>
</div>
<div class="test3" name="test3">
<div class="test8" name="test8"> </div>
</div>
<div class="test4" name="test4">
<div class="test9" name="test9"> </div>
</div>
<div class="test5" name="test5">
<div class="test1" name="test1"> </div>
</div>
</div>
var i, imax;
var doc = document;
var formelem = doc.getElementById('testform').elements;
for (i = 0, imax = formelem.length; i < imax; i += 1)
{
var elemvalue = formelem[i].value;
}
var formelem = doc.getElementById('testdiv').getElementsByTagName('input');
for (i = 0, imax = formelem.length; i < imax; i += 1)
{
var elemvalue = formelem[i].value;
}
var formelem = doc.getElementById('testdiv').querySelectorAll('[data-unique]');
for (i = 0, imax = formelem.length; i < imax; i += 1)
{
var elemvalue = formelem[i].value;
}
var formelem = doc.getElementById('testdiv').getElementsByClassName('unique');
for (i = 0, imax = formelem.length; i < imax; i += 1)
{
var elemvalue = formelem[i].value;
}
var formelem = doc.getElementsByName('unique');
for (i = 0, imax = formelem.length; i < imax; i += 1)
{
var elemvalue = formelem[i].value;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
form.elements | |
getElementsByTagName | |
querySelectorAll | |
getElementsByClassName | |
getElementsByName |
Test name | Executions per second |
---|---|
form.elements | 3097062.2 Ops/sec |
getElementsByTagName | 2617286.2 Ops/sec |
querySelectorAll | 779798.8 Ops/sec |
getElementsByClassName | 1930128.5 Ops/sec |
getElementsByName | 2524947.2 Ops/sec |
Let's break down the provided benchmark and explain what is being tested, compared options, their pros and cons, and other considerations.
What is being tested?
The benchmark compares four ways to retrieve form elements from an HTML document:
form.elements
(a property of the document
object)getElementsByTagName('input')
querySelectorAll('[data-unique]')
getElementsByClassName('unique')
Options comparison
The four options are compared in terms of performance, specifically the number of executions per second.
elements
property of the document
object to retrieve all form elements.getElementsByTagName()
method to retrieve all input elements within a specific element (in this case, the testdiv
element).querySelectorAll()
method to retrieve all elements that match the specified CSS selector ([data-unique]
).getElementsByClassName()
method to retrieve all elements with a specific class name (unique
).Pros and cons of each approach
Here's a brief summary of the pros and cons of each approach:
form.elements
due to the overhead of method calls.Other considerations
querySelectorAll
and getElementsByClassName
, which are part of the DOM API. These libraries provide a convenient way to select elements using CSS selectors and class names.Benchmark results
The benchmark results show the number of executions per second for each option:
Based on these results, getElementsByClassName('unique')
appears to be the fastest option, followed closely by getElementsByTagName('input')
.