let i = 1001;
const results = [];
while (--i) {
const index = i;
window.addEventListener('custom:test', () => {
results.push('Native Event listener ' + index);
}, {once: true});
}
window.dispatchEvent(new CustomEvent('custom:test', {bubbles: false}));
let i = 1001;
const callbacks = [];
const results = [];
while (--i) {
const index = i;
callbacks.push(() => {
results.push('Fake listeners array ' + index);
});
}
window.addEventListener('custom:test', () => {
results.push('Native Event listener ' + index);
let i = callbacks.length;
while (--i) callbacks[i]();
}, {once: true});
window.dispatchEvent(new CustomEvent('custom:test', {bubbles: false}));
let i = 1001;
const target = new EventTarget();
const results = [];
while (--i) {
const index = i;
target.addEventListener('custom:test', () => {
results.push('Custom EventTarget ' + index);
}, {once: true});
}
target.dispatchEvent(new CustomEvent('custom:test', {bubbles: false}));
let i = 1001;
const callbacks = [];
const target = new EventTarget();
const results = [];
while (--i) {
const index = i;
callbacks.push(() => {
results.push('Fake listeners array ' + index);
});
}
target.addEventListener('custom:test', () => {
results.push('Native Event listener ' + index);
let i = callbacks.length;
while (--i) callbacks[i]();
}, {once: true});
target.dispatchEvent(new CustomEvent('custom:test', {bubbles: false}));
let i = 1001;
const callbacks = [];
const results = [];
while (--i) {
const index = i;
callbacks.push(() => {
results.push('Fake listeners array ' + index);
});
}
i = callbacks.length;
while (--i) callbacks[i]();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Native Event listeners | |
Native Event listener with callbacks array | |
Custom EventTarget | |
Custom EventTarget with callbacks array | |
Fake listeners array |
Test name | Executions per second |
---|---|
Native Event listeners | 341.8 Ops/sec |
Native Event listener with callbacks array | 11287.4 Ops/sec |
Custom EventTarget | 378.5 Ops/sec |
Custom EventTarget with callbacks array | 11822.0 Ops/sec |
Fake listeners array | 42331.5 Ops/sec |
Let's break down the benchmark and its test cases.
Benchmark Definition
The benchmark measures the performance of three approaches for handling event listeners:
window
object using the addEventListener
method.EventTarget
instance and adding an event listener to it using the addEventListener
method.Options Compared
The benchmark compares the performance of these three approaches:
Pros and Cons
Here's a brief summary of the pros and cons of each approach:
EventTarget
instance, which can add overhead.Library and Purpose
The EventTarget
class is a built-in JavaScript class that represents an object that can emit events. It's used in web browsers to manage events and allow for more control over event handling.
Special JS Feature/Syntax
None of the test cases use any special JavaScript features or syntax that would require additional explanation.
Other Alternatives
If you're interested in exploring alternative approaches, here are a few:
event-listener
to handle events in a more lightweight and efficient way.Keep in mind that these alternatives may not be relevant to the specific use case of this benchmark, but they can provide interesting insights into alternative design choices.