Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
1000 x event listeners vs single event listener with array of 1000 callbacks
(version: 0)
Comparing performance of:
1000 event listeners vs 1 event listener handling an array of 1000 callbacks
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
1000 event listeners
let i = 1001; const results = []; while (--i) { const index = i; window.addEventListener('custom:test', () => { results.push(index); }, {once: true}); } window.dispatchEvent(new CustomEvent('custom:test'));
1 event listener handling an array of 1000 callbacks
let i = 1001; const results = []; const callbacks = []; while (--i) { const index = i; callbacks.push(() => { results.push(index); }) } window.addEventListener('custom:test', () => { callbacks.forEach((cb) => { cb() }) }, {once: true}); window.dispatchEvent(new CustomEvent('custom:test'));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
1000 event listeners
1 event listener handling an array of 1000 callbacks
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:144.0) Gecko/20100101 Firefox/144.0
Browser/OS:
Firefox 144 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
1000 event listeners
409.7 Ops/sec
1 event listener handling an array of 1000 callbacks
50412.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided benchmark measures the performance difference between two approaches to handling an array of callbacks in JavaScript: 1. **Individual Event Listeners**: Each callback is added as a separate event listener, with `window.addEventListener` being called 1000 times with different indices. 2. **Single Event Listener with Array of Callbacks**: A single event listener is created and passed an array of callbacks to handle. **Comparison** The benchmark compares the performance of these two approaches: * **Individual Event Listeners**: The test creates 1000 separate event listeners, each bound to a specific callback function. * **Single Event Listener with Array of Callbacks**: The test creates a single event listener and passes an array of callbacks to it. **Pros and Cons** ### Individual Event Listeners Pros: * Easy to implement and understand * Allows for fine-grained control over each callback Cons: * Creates 1000 separate objects in memory, leading to increased memory usage * May lead to performance issues due to the overhead of creating and removing event listeners ### Single Event Listener with Array of Callbacks Pros: * Reduces memory usage compared to individual event listeners * Can be more efficient since only one event listener needs to be created and managed Cons: * Requires a single function that can handle all callbacks, which may lead to increased complexity * May not provide the same level of control over each callback as individual event listeners **Library and Special JS Feature** The benchmark uses the `CustomEvent` API, which is a standard feature in modern browsers. A `CustomEvent` is an event that is defined by the developer, but is still handled by the browser. No special JavaScript features or syntax are used in this benchmark. **Other Alternatives** Alternative approaches to handling arrays of callbacks might include: * **Using a callback queue**: Instead of adding all callbacks to an array and passing it to a single function, a callback queue can be implemented to process callbacks one by one. * **Using a library like Lodash**: Lodash provides a `forEach` function that can be used to iterate over arrays, eliminating the need for individual event listeners. * **Using a framework or async/await**: Some frameworks and libraries provide built-in support for handling arrays of callbacks in an asynchronous manner. In summary, the benchmark compares two approaches to handling arrays of callbacks in JavaScript: individual event listeners vs. a single event listener with an array of callbacks. The choice between these approaches depends on factors such as memory usage, performance requirements, and code complexity.
Related benchmarks:
Array loop vs foreach vs map (really large set)
Array loop vs foreach vs map with 100000 itens on array
Array loop vs foreach vs map with large array
Array loop vs foreach vs map 100 000
Array loop vs foreach vs every
Comments
Confirm delete:
Do you really want to delete benchmark?