Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Custom Event vs Callback 2
(version: 0)
Comparing performance of:
Custom Event vs Callback
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Custom Event
let i = 1001; const results = []; window.addEventListener('custom:test', (evt) => { results.push(`Custom Event ${evt.detail.idx}`); }); while (--i) { window.dispatchEvent(new CustomEvent('custom:test', { detail: { idx: i } })); }
Callback
const results = []; function cb(i) { results.push(`Callback ${i}`); } let i = 1001; while (--i) { cb(i); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Custom Event
Callback
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Custom Event
31.4 Ops/sec
Callback
91802.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared, and other considerations. **Benchmark Overview** The test case compares two approaches to handle events: Custom Events and Callback functions. **Custom Events** * Library: None * Purpose: Simulate an event using `CustomEvent` constructor. * Test Case: ```javascript window.addEventListener('custom:test', (evt) => { results.push(`Custom Event ${evt.detail.idx}`); }); while (--i) { window.dispatchEvent(new CustomEvent('custom:test', { detail: { idx: i } })); } ``` Here, an event listener is attached to the `window` object with a custom event name 'custom:test'. When this event is dispatched using `window.dispatchEvent()`, the callback function receives the event details (idx) and pushes it to the results array. **Callback Functions** * Library: None * Purpose: Simulate an event by calling a callback function repeatedly. * Test Case: ```javascript const results = []; function cb(i) { results.push(`Callback ${i}`); } let i = 1001; while (--i) { cb(i); } ``` Here, a simple callback function `cb()` is defined, and it pushes the event details (idx) to the results array. The loop iterates from 1001 down to 0, calling the callback function on each iteration. **Comparison** The test compares the performance of these two approaches: * Custom Events: Simulate an event using a custom event constructor. * Callback Functions: Simulate an event by calling a callback function repeatedly. **Pros and Cons** * **Custom Events**: + Pros: - Easy to use and understand. - Can be easily delegated to other components or libraries. + Cons: - May require additional setup (e.g., defining the custom event type). - Can lead to tight coupling between components that use it. * **Callback Functions**: + Pros: - Lightweight and easy to implement. - Can be used with existing event handling mechanisms. + Cons: - Can lead to "callback hell" (nested callback functions) if not managed properly. **Other Considerations** * In a real-world scenario, you might need to consider factors like event dispatching overhead, event listener registration, and cleanup when using custom events or callbacks. * For complex event handling scenarios, libraries like jQuery or React's event handling mechanisms might be more suitable and efficient. * The test doesn't account for other factors that could affect performance, such as caching, memoization, or optimizing the event handling mechanism. **Alternatives** If you're looking for alternatives to these approaches: * **Event Delegation**: Instead of using custom events or callbacks, you can use event delegation to handle events. This approach involves attaching a single event listener to a parent element and then listening for events on its child elements. * **Promise-based Event Handling**: You can use promise-based event handling mechanisms like `Promise.all()` or `Promise.race()` to handle multiple events concurrently. * **Library-based Solutions**: Consider using libraries like jQuery, React, or Angular, which provide efficient and robust event handling mechanisms. Keep in mind that these alternatives might add complexity to your codebase, so it's essential to weigh the benefits against the trade-offs.
Related benchmarks:
Custom Event vs Callback with console log
Getting a context: closest vs while loop vs event propagation with new events vs event propagation with callbacks
Getting a context: while loop vs event propagation with new events vs event propagation with callbacks
Custom Event vs Callback [fixed - apples to apples]
Custom Event vs Callback with console log large
Comments
Confirm delete:
Do you really want to delete benchmark?