<div class="test-wrapper">
<div class="test test-class"></div>
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js'></script>
document.querySelector(".test");
$(document).find(".test");
document.querySelector(".test").classList.add("test-add");
$(document).find(".test").addClass("test-add");
document.querySelector(".test").classList.remove("test-class");
$(document).find(".test").removeClass("test-class");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Get element by class - Vanilla | |
Get element by class - JQuery | |
Add class - Vanilla | |
Add class - JQuery | |
Remove class - Vanilla | |
Remove class - JQuery |
Test name | Executions per second |
---|---|
Get element by class - Vanilla | 2468742.5 Ops/sec |
Get element by class - JQuery | 373015.8 Ops/sec |
Add class - Vanilla | 936521.0 Ops/sec |
Add class - JQuery | 186312.1 Ops/sec |
Remove class - Vanilla | 983739.6 Ops/sec |
Remove class - JQuery | 206023.4 Ops/sec |
Let's break down the benchmark and its components.
Benchmark Definition:
The benchmark is defined by two main parts:
.test
, which will be used as the target element for each test case.Individual Test Cases:
There are six test cases, each measuring a different aspect of performance between vanilla JavaScript and jQuery:
Library Used:
In this benchmark, jQuery is used as the library for comparison with vanilla JavaScript. The purpose of jQuery is to provide a simpler and more convenient way to manipulate HTML documents, compared to vanilla JavaScript.
Special JS Feature/Syntax:
None of the test cases use any special JavaScript features or syntax that requires specific explanation.
Pros and Cons of Different Approaches:
Here's a brief summary of the pros and cons of using jQuery versus vanilla JavaScript for each test case:
Other Alternatives:
If you wanted to measure the performance of other JavaScript libraries or alternatives, such as:
_.get(element, 'class')
).DOMParser
, CSSOM
, or modern browsers' built-in APIs (e.g., Element.prototype.classList
) can also be used for similar tasks.Keep in mind that each of these alternatives would require modifications to the benchmark definition and test cases, as well as additional analysis to determine their performance characteristics.