<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.0.min.js"></script>
<div class="myDiv"></div>
function getElement(selector, single, pos) {
var el;
// return single node
// only runs if [single] is undefined
if(single == undefined) el = document.getElementById(selector) || document.querySelector(selector);
if(el == null) {
if(single == false) {
// return nodelist
el = document.getElementsByClassName(selector);
if(el.length == 0) el = document.getElementsByTagName(selector);
if(el.length == 0) el = document.querySelectorAll(selector);
if(el.length == 0) el = getElementsByClassName(selector);
} else {
// return single node from nodelist
pos = (pos == null) ? 0 : pos; // if arrayPos null, default to 0
el = document.getElementsByClassName(selector)[pos] ||
document.getElementsByTagName(selector)[pos] ||
document.querySelectorAll(selector)[pos] ||
getElementsByClassName(selector)[pos];
}
}
return el;
}
var el = document.getElementsByClassName('myDiv')[0];
var classname = el.className;
var el = $('.myDiv')[0];
var classname = el.className;
var el = getElement('myDiv');
var classname = el.className;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
vanilla js | |
jquery | |
custom |
Test name | Executions per second |
---|---|
vanilla js | 7745150.0 Ops/sec |
jquery | 1790880.4 Ops/sec |
custom | 515416.0 Ops/sec |
Let's break down the provided benchmark definition and test cases.
Benchmark Definition:
The benchmark measures the performance of getting an element by its class name using three different approaches:
var el = document.getElementsByClassName('myDiv')[0]; var classname = el.className;
var el = $('.myDiv')[0]; var classname = el.className;
var el = getElement('myDiv'); var classname = el.className;
where getElement
is a custom function defined in the Script Preparation Code.Script Preparation Code:
The custom getElement
function is designed to handle different cases for getting an element by its class name. Here's what it does:
single
is undefined, it uses document.getElementById(selector)
or document.querySelector(selector)
to get a single element.document.getElementsByClassName(selector)
.document.getElementsByTagName(selector)
(which is not recommended for CSS classes).document.querySelectorAll(selector)
(a modern DOM method).getElementsByClassName(selector)
(an older, deprecated method).Pros and Cons:
Other Considerations:
getElement
function uses an older method (getElementsByClassName
) which is deprecated in modern browsers. You may want to consider updating this approach.jQuery Library:
In this benchmark, jQuery is used as a wrapper around the vanilla JavaScript DOM methods. This allows for a more efficient and convenient way to interact with the document. However, it also means that you need to include an additional library file (jquery-3.1.0.min.js
) which may have some overhead.
Special JS Feature/Syntax:
There is no special JavaScript feature or syntax mentioned in this benchmark. The code uses standard DOM methods and jQuery wrappers.
Now, regarding the alternative approaches:
document.querySelector
with a CSS selector (e.g., .myDiv
.document.querySelectorAll
to get all elements with the specified class.Keep in mind that these alternatives may have different performance characteristics and usage patterns compared to the benchmarked approaches.