<script src="//ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="outer">
<div class="inner"></div>
</div>
const $outer = $('.outer');
$('.outer .inner').on('click', function (e) {});
$outer.find('.inner').on('click', function (e) {});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
jQuery raw | |
jQuery find |
Test name | Executions per second |
---|---|
jQuery raw | 662068.9 Ops/sec |
jQuery find | 1036004.8 Ops/sec |
The benchmark you've provided compares two methods for selecting DOM elements and attaching event handlers using the jQuery library. Specifically, it evaluates the performance difference between using a raw jQuery selector and using a stored jQuery object. Here’s a breakdown of the benchmark and the implications of each approach.
jQuery raw
$('.outer .inner').on('click', function (e) {});
$('.outer .inner')
is called directly, which searches the DOM each time it executes to find the specified elements. This means that every time the code runs, jQuery needs to perform a full search for the elements.jQuery find
$outer.find('.inner').on('click', function (e) {});
$outer
is a jQuery object that has been prepared beforehand to reference the outer element (the .outer
div). The .find()
method is then used on that object to locate the .inner
div. Since $outer
already references the DOM element, the .find()
method can operate on a smaller context, potentially making the search more efficient.The benchmark results show that the "jQuery find" method had a significantly higher number of executions per second (1,036,004.81) compared to the "jQuery raw" method (662,068.94). This indicates that using a cached jQuery object and the .find()
method is more efficient in terms of performance:
jQuery find (cached object):
jQuery raw (direct selector):
Readability: While both methods are readable, the raw jQuery selector may be easier for quick checks and simple scenarios. However, for applications where performance is critical, especially in event-heavy interactions, caching DOM queries is advantageous.
Event Handling in jQuery: The example provided uses jQuery to attach an event handler to elements. Using .on()
is a powerful feature of jQuery that allows for delegated event handling, making it easy to manage events dynamically.
Library Use: jQuery is a widely-used JavaScript library designed to simplify HTML document manipulation, event handling, and AJAX interactions. While jQuery offers great convenience, modern JavaScript (ES6 and beyond) and frameworks like React, Vue, or Angular are often preferred for new projects due to their more robust approaches to state management and performance optimizations.
There are various alternatives to using jQuery for DOM manipulation and event handling, especially with the evolution of modern JavaScript:
Vanilla JavaScript: Utilizing native DOM methods such as document.querySelector()
and addEventListener()
. While less concise than jQuery, it eliminates the overhead of loading an entire library and can be optimized for performance.
Modern Frameworks: Using frameworks or libraries like React, Vue, or Angular allows for structured and efficient component-based architectures that manage state and updates better than jQuery in complex applications.
Custom Utility Functions: Some developers may create their utility functions that streamline specific tasks without relying on a large library like jQuery.
In conclusion, the benchmark highlights the performance benefits of caching DOM references while also showcasing the trade-offs between simplicity and efficiency in event handling. For performance-critical applications, employing the caching strategy is advisable.