<div id="testElement"></div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js'></script>
var el = $("#testElement")[0];
var className = el.className;
var el = document.getElementById('testElement');
var className = el.className;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
jQuery | |
Vanilla JS |
Test name | Executions per second |
---|---|
jQuery | 1168111.6 Ops/sec |
Vanilla JS | 2947566.2 Ops/sec |
I'll explain what's being tested on the provided JSON.
The benchmark is comparing the speed of two approaches to retrieve an element by its ID:
id
attribute using $()
and then accesses the className
property of the resulting object.Now, let's discuss the pros and cons of each approach:
jQuery:
Pros:
$(element).addClass('class')
)Cons:
Vanilla JS:
Pros:
Cons:
In terms of special JavaScript features or syntax, neither test case uses any advanced features. However, it's worth noting that the benchmark does not include a third option, such as using document.querySelector()
(introduced in ECMAScript 2015), which can provide similar functionality to jQuery but with better performance and fewer dependencies.
As for other alternatives, here are a few examples:
These alternatives are not included in the benchmark, as they represent more comprehensive frameworks rather than simple DOM manipulation libraries like jQuery.