for (i=0; i<10000;i++) {
document.addEventListener('click', ()=>{})
}
for (i=0; i<10000;i++) {
$('document').on('click', function(){})}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
addEvenListener | |
.on |
Test name | Executions per second |
---|---|
addEvenListener | 0.6 Ops/sec |
.on | 26.5 Ops/sec |
I'd be happy to help you understand what's being tested in the provided benchmark.
What is being tested?
The benchmark tests two approaches for attaching an event listener to the document
object: addEventListener()
and .on()
(also known as jQuery's "on" method). The test case creates a loop that runs 10,000 times, adding an event listener with no callback function.
Options compared
There are two options being compared:
addEventListener()
: This is the native JavaScript method for attaching an event listener to an element or document. It takes three arguments: the type of event, the target element, and a callback function..on()
(jQuery's "on" method): This is a part of the jQuery library, which is a popular JavaScript library for DOM manipulation and event handling. The .on()
method also takes three arguments: the type of event, the selector or target element, and a callback function.Pros and Cons
addEventListener()
:
Pros:
.on()
method.Cons:
.on()
(jQuery's "on" method):
Pros:
Cons:
addEventListener()
.Other considerations
When choosing between these two approaches, consider the following factors:
addEventListener()
support, .on()
might be a safer choice.addEventListener()
will likely be faster than using .on()
..on()
might be a more convenient choice. Otherwise, addEventListener()
is generally the better option.Library usage
The test case uses jQuery's .on()
method, which suggests that the author wants to compare its performance with native JavaScript's addEventListener()
method.
Special JS feature or syntax
There are no special features or syntaxes being tested in this benchmark. Both approaches use standard JavaScript and jQuery methods.
I hope this explanation helps you understand what's being tested in the provided benchmark!