<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.0.min.js"></script>
<div id="testElement"></div>
function getElement(a,b,c){var d;return void 0==b&&(d=document.getElementById(a)||document.querySelector(a)),null==d&&(0==b?(d=document.getElementsByClassName(a),0==d.length&&(d=document.getElementsByTagName(a)),0==d.length&&(d=document.querySelectorAll(a)),0==d.length&&(d=getElementsByClassName(a))):(c=null==c?0:c,d=document.getElementsByClassName(a)[c]||document.getElementsByTagName(a)[c]||document.querySelectorAll(a)[c]||getElementsByClassName(a)[c])),d}
var el = getElementById("testElement");
var className = el.className;
var el = $("#testElement")[0];
var className = el.className;
var el = getElement("testElement");
var className = el.className;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
vanilla | |
jquery | |
custom function |
Test name | Executions per second |
---|---|
vanilla | 0.0 Ops/sec |
jquery | 521447.5 Ops/sec |
custom function | 1763947.6 Ops/sec |
Let's break down the benchmark and explain what is tested, the pros and cons of each approach, and other considerations.
Benchmark Definition
The benchmark definition provides a custom JavaScript function called getElement(a, b, c)
that is used to retrieve an element from the DOM. The function takes three parameters: the ID or class name of the element (a
), the index of the element among its siblings with the same class name (b
), and the document (c
) itself.
Options Compared
The benchmark compares three different approaches for retrieving an element by its class name:
getElementById()
method, which is a native DOM method that returns the first element with the specified ID.$
function from the jQuery library, which is used to select elements based on their class names or IDs.getElement(a, b, c)
that uses various methods to retrieve an element by its class name.Pros and Cons of Each Approach
Library and Syntax
The getElement()
custom function uses various methods to retrieve an element by its class name, including:
document.getElementById(a)
(vanilla)$
function from jQuery ($(a)[0]
)document.getElementsByClassName(a)
, document.getElementsByTagName(a)
, or document.querySelectorAll(a)
(custom implementation)Special JS Features or Syntax
There is no special JavaScript feature or syntax used in this benchmark. The custom function uses standard DOM methods, and the jQuery method relies on the $
function from the jQuery library.
Other Considerations
<div id="testElement"></div>
) to test the retrieval of an element by its class name.Alternatives
Other alternatives for retrieving an element by its class name include:
document.querySelector(a)
: A modern DOM method that returns the first matching element based on the specified selector.document.querySelectorAll(a)
: A DOM method that returns a NodeList of all matching elements based on the specified selector.element.getAttribute("class")
.These alternatives may offer different trade-offs in terms of performance, complexity, and compatibility with various browsers and environments.