HTML Preparation code:
AخA
 
1
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.0.min.js"></script>
2
<div class="myDiv"></div>
Script Preparation code:
x
 
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;
}
Tests:
  • vanilla js

     
    var el = document.getElementsByClassName('myDiv')[0];
    var classname = el.className;
  • jquery

     
    var el = $('.myDiv')[0];
    var classname = el.className;
  • custom

     
    var el = getElement('myDiv');
    var classname = el.className;
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    vanilla js
    jquery
    custom

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: one year ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Chrome 123 on Windows
View result in a separate tab
Test name Executions per second
vanilla js 7745150.0 Ops/sec
jquery 1790880.4 Ops/sec
custom 515416.0 Ops/sec
Related benchmarks: