Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
AddEventListener vs direct
(version: 1)
Comparing performance of:
Add vs Direct
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="foo"></div>
Tests:
Add
var element = document.getElementById("foo"); element.addEventListener('click', () => console.log(''));
Direct
var element = document.getElementById("foo"); element.onclick = () => console.log('');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Add
Direct
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Add
2611.1 Ops/sec
Direct
2649.8 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark represented in the provided JSON tests two different methods of attaching event listeners to a DOM element, specifically a `click` event to a `<div>` with the ID "foo". The two approaches being compared are: 1. **Using `addEventListener` (named "Add")** 2. **Using direct assignment of `onclick` (named "Direct")** ### Comparison of Methods #### 1. `addEventListener` - **Code Example:** ```javascript var element = document.getElementById("foo"); element.addEventListener('click', () => console.log('')); ``` - **Pros:** - **Multiple Handlers:** You can attach multiple event handlers for the same event type on the same element without overwriting any previous handlers. - **Event Options:** It supports additional options like `capture`, `passive`, and `once`, allowing more control over event propagation and performance. - **Cons:** - Slightly more overhead due to the function call and additional options handling. This could lead to marginally reduced performance in scenarios where maximum efficiency is needed. #### 2. Direct Assignment (`onclick`) - **Code Example:** ```javascript var element = document.getElementById("foo"); element.onclick = () => console.log(''); ``` - **Pros:** - **Simplicity:** This method is straightforward and easier to understand for basic use cases. - **Performance:** It may show better performance in specific scenarios, as it does not involve additional function overhead. The benchmark results show `onclick` as having marginally higher executions per second compared to `addEventListener`. - **Cons:** - **Single Handler:** If you assign a new function to `onclick`, it overwrites any existing click handler, which can lead to unexpected behavior in more complex applications where multiple handlers are required. - **Limited Options:** You cannot specify additional options for event handling. ### Additional Considerations - The benchmark results indicate that `onclick` had a slightly higher performance (2649.77 executions per second) compared to `addEventListener` (2611.08 executions per second) under the tested conditions. - Performance can vary based on the specific browser implementation and the context in which the benchmarks are run. Thus, while performance is an important factor, code maintainability and future-proofing should also be considered when choosing between these methods. ### Alternatives Besides the two methods being benchmarked, other alternatives for handling events in JavaScript include: - **Using jQuery (or similar libraries):** ```javascript $('#foo').on('click', function() { console.log(''); }); ``` This library provides a concise syntax for event management among other features but adds the overhead of including the library itself. - **Custom Events:** For more robust applications, developers may opt to create and dispatch custom events, which allow for more intricate event handling scenarios. - **Framework-specific Solutions:** If working with frameworks like React, Angular, or Vue, events are handled through their respective methodologies (e.g., JSX in React for event handling), which abstract away direct DOM manipulation for better performance and maintainability. In conclusion, the choice between `addEventListener` and `onclick` should be influenced by factors such as the need for multiple event handlers, specific options, code maintainability, and performance needs in the context of the application being developed.
Related benchmarks:
querySelector vs getElementById
querySelector vs getElementById
classList vs 2 classList
className vs classList by Tyler
className vs. className
setAttribute, classList
test12xxxx2
test123xxxxx
AddEventListener vs direct v2
Comments
Confirm delete:
Do you really want to delete benchmark?