Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Cost of adding and removing event listeners
(version: 1)
Comparing performance of:
Assigning once vs Assigning 1000
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const callbacks = Array.from({ length: 1000 }, () => () => {}) const callback = () => {} function useEffectCallback(callback) { window.addEventListener('keydown', callback); window.removeEventListener('keydown', callback); }
Tests:
Assigning once
useEffectCallback(callback)
Assigning 1000
callbacks.forEach(callback => useEffectCallback(callback))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Assigning once
Assigning 1000
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0
Browser/OS:
Chrome 143 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Assigning once
599394.8 Ops/sec
Assigning 1000
656.8 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON focuses on measuring the performance cost associated with adding and removing event listeners in JavaScript. It specifically examines two scenarios through different test cases: 1. **Assigning once**: This test involves calling the `useEffectCallback` function once with a single callback function. The structure of the benchmark allows for the comparison of the performance impact when only one event listener is added to the `keydown` event. 2. **Assigning 1000**: In this scenario, the `useEffectCallback` is called 1,000 times, each time assigning a different callback function to the `keydown` event. This test evaluates how performance degrades when multiple event listeners are added and subsequently removed. ### Pros and Cons of the Approaches #### `useEffectCallback(callback)` – Assigning once - **Pros**: - Lower overhead since only one event listener is managed. - Simpler to implement and understand; involves just a single attachment and detachment. - Enhanced performance, as evidenced by the test result of approximately 2.8 million executions per second, indicating that the operation is efficient for this case. - **Cons**: - Limited use case; may not be flexible enough if multiple, different event handling is needed. #### `callbacks.forEach(callback => useEffectCallback(callback))` – Assigning 1000 - **Pros**: - Useful for scenarios where multiple event listeners are required for different functionalities. - Allows the handling of multiple specific events or listener behavior. - **Cons**: - Significant performance degradation, as shown by the result of about 2,972 executions per second, indicating much higher overhead when managing many listeners. - Complexity increases in managing multiple event listeners, which can introduce bugs, especially when adding and removing them. ### Library and Features Used The benchmark does not explicitly rely on any external libraries and instead utilizes native JavaScript functionality provided by the `window` object. Specifically, it uses the `addEventListener` and `removeEventListener` methods to manage event listeners. These methods are part of the standard DOM API and allow for efficient interaction with events in the browser. ### Other Considerations - **Event Listener Management**: In practice, it is essential to balance the number of event listeners to ensure good performance while still handling user interactions adequately. - **Memory Leaks**: Poor handling of event listeners can lead to memory leaks, especially if listeners are not removed properly when no longer needed. ### Alternatives - **Single Event Handler**: Instead of attaching multiple listeners, consider using a single event handler that determines what action to perform based on the event state or properties. - **Debouncing or Throttling**: Techniques that can help manage the frequency of event handler executions, especially for events that fire frequently (e.g., scroll or resize). - **Delegated Event Handling**: This involves placing the event listener on a parent element rather than on individual child elements, reducing the total number of listeners and improving performance. In summary, the benchmark effectively highlights the trade-offs in JavaScript when adding event listeners, illustrating the impact on performance based on the number of listeners involved while simultaneously emphasizing the best practices for managing these event handlers efficiently.
Related benchmarks:
Event listener types - 1000x native vs 10000x fake
Native Event listener vs custom EventTarget vs fake listeners array
Custom Event vs Callback 2
event listeners window vs input/element via 1 element
event listener test: window vs element - listeners in preparation code
1000 x event listeners vs single event listener with array of 1000 callbacks
_performance_comparison: DeepClone vs removing/adding eventlisteners
Cost of adding and removing events
AddEventListener vs direct v2
Comments
Confirm delete:
Do you really want to delete benchmark?