Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
EventTarget vs custom observable implementation
(version: 1)
Compares browser's built-in "EventTarget" with custom similar implementation
Comparing performance of:
custom vs native
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
class CustomTarget { constructor() { this.listeners = {}; } on(eventName, listener) { if (!(eventName in this.listeners)) { this.listeners[eventName] = new Set(); } this.listeners[eventName].add(listener); } off(eventName, listener) { this.listeners[eventName]?.delete(listener); } send(eventName, ...data) { if (!this.listeners[eventName]) { return 0; } for (let fn of this.listeners[eventName]) { fn(...data); } return this.listeners[eventName].count; } } const listeners = 10; const events = 1000; const setupCustom = () => { const target = new CustomTarget(); let total = 0; for (let i = 0; i < listeners; i++) { target.on('foo', (inc) => { total = total + inc }); } return () => { for (let i = 0; i < events; i++) { target.send('foo', 5); } } } const setupNative = () => { const target = new EventTarget(); let total = 0; for (let i = 0; i < listeners; i++) { target.addEventListener('foo', (e) => { total = total + e.detail }); } return () => { for (let i = 0; i < events; i++) { const customEvent = new CustomEvent('foo', { detail: 5 }); target.dispatchEvent(customEvent); } } } const runCustomInt = setupCustom(); const runNative = setupNative(); function runCustom() { runCustomInt(); }
Tests:
custom
runCustom();
native
runNative();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
custom
native
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
15 days ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:149.0) Gecko/20100101 Firefox/149.0
Browser/OS:
Firefox 149 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
custom
3494.1 Ops/sec
native
280.2 Ops/sec
Related benchmarks:
Observables: loops versus EventTarget
Observables: loops versus EventTarget (3 listeners)
Observables: loops versus EventTarget2
Native Event listener vs custom EventTarget vs fake listeners array
Observables: loops versus EventTarget vs extended event
Custom Event vs PubSub
Custom Event vs PubSub Single Event
Observables: loops with try/catch versus EventTarget
Custom Event vs PubSub Single Event 10
Comments
Confirm delete:
Do you really want to delete benchmark?