<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js'></script>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
document.querySelectorAll('div').forEach((e) => {
e.onmouseenter = '';
});
var divs = document.querySelectorAll('div');
const f = () => { return 123; };
divs.forEach((e) => {
e.addEventListener('click', f);
});
divs.forEach((e) => {
e.addEventListener('click', () => { return 123; });
});
const f = () => { return 123; };
document.querySelectorAll('div').forEach((e) => {
jQuery(e).on('click', f);
});
document.querySelectorAll('div').forEach((e) => {
jQuery(e).on('click', () => { return 123; });
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
addEventListener | |
addEventListener anon | |
jQuery.on() | |
jQuery.on() anon |
Test name | Executions per second |
---|---|
addEventListener | 106.6 Ops/sec |
addEventListener anon | 50.1 Ops/sec |
jQuery.on() | 17952.0 Ops/sec |
jQuery.on() anon | 16316.8 Ops/sec |
Benchmark Overview
The provided benchmark compares the performance of three approaches to attach an event listener to HTML elements: addEventListener()
with and without anonymous functions, and jQuery.on()
with and without anonymous functions.
Test Cases
There are four test cases:
addEventListener
: Attaches a named function as an event listener.addEventListener anon
: Attaches an anonymous function as an event listener (equivalent to passing a callback function).jQuery.on()
: Attaches an event listener using jQuery's on()
method with a named function.jQuery.on() anon
: Attaches an event listener using jQuery's on()
method with an anonymous function.Comparison
The benchmark measures the number of executions per second for each test case. The results show that:
addEventListener anon
outperforms all other approaches, suggesting that attaching anonymous functions is more efficient.jQuery.on()
anon** performs similarly to **
addEventListener anon**, implying that passing an anonymous function to
on()has a similar performance profile to attaching a named function with
addEventListener`.jQuery.on()
and addEventListener
have different performance profiles, suggesting that the jQuery approach may be slower due to additional overhead.Pros and Cons
addEventListener anon
:jQuery.on()
anon:addEventListener anon
, with additional features like event delegation.jQuery.on()
:Library Usage
The jQuery
library is used in tests 3 and 4, providing a convenient way to attach event listeners with minimal code. However, this also introduces an additional dependency that may affect performance.
Special JS Features/Syntax
None of the test cases use any special JavaScript features or syntax that would require additional explanation.
Alternatives
If you need alternative approaches for attaching event listeners, consider:
addEventListener
: A lightweight and widely supported approach.Keep in mind that the choice of approach ultimately depends on your specific use case, performance requirements, and personal preference.