Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
EventTarget vs custom observable implementation
Compares browser's built-in "EventTarget" with custom similar implementation
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:137.0) Gecko/20100101 Firefox/137.0
Browser:
Firefox 137
Operating system:
Linux
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
custom
6533.7 Ops/sec
native
426.1 Ops/sec
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();