Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Custom Event vs PubSub Single Event 10
(version: 0)
Comparing performance of:
Custom Event vs PubSub
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
Custom Event
let i = 10; const results = []; while (--i) { window.dispatchEvent(new CustomEvent('custom:test')); }
PubSub
class PubSub { constructor() { this.events = {}; } subscribe(event, callback) { if (!this.events.hasOwnProperty(event)) { this.events[event] = []; } return this.events[event].push(callback); } publish(event, data = {}) { if (!this.events.hasOwnProperty(event)) { return []; } return this.events[event].map((callback) => callback(data)); } } const pubSub = new PubSub(); let i = 10; const results = []; while (--i) { pubSub.publish('custom', 'test') }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Custom Event
PubSub
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:126.0) Gecko/20100101 Firefox/126.0
Browser/OS:
Firefox 126 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Custom Event
115518.0 Ops/sec
PubSub
568326.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is comparing two approaches: `Custom Event` and `PubSub Single Event 10`. The purpose of this benchmark is to measure the performance difference between using a custom event and a pub/sub (publish-subscribe) pattern in JavaScript. **Options Compared** 1. **Custom Event**: This approach uses the `window.dispatchEvent()` method to dispatch a custom event with a specific name ("custom:test"). The event is dispatched 10 times in a loop, where the loop counter (`i`) decreases from 10 to 0. 2. **PubSub Single Event 10**: This approach uses a pub/sub pattern implemented as a class `PubSub`. The `subscribe()` method is used to attach callbacks to specific events, and the `publish()` method is used to publish events with data. **Pros and Cons** 1. **Custom Event**: * Pros: Simple and lightweight, doesn't require additional library or setup. * Cons: May not be as efficient due to the need for event dispatching and handling in JavaScript engines. 2. **PubSub Single Event 10**: * Pros: Can handle more complex event structures and may be more efficient due to optimized event handling. * Cons: Requires additional library or setup (the `PubSub` class) and may have overhead due to the use of a class. **Library/Class Used** The `PubSub` class is used in the "PubSub" test case. This class implements a basic pub/sub pattern, allowing users to subscribe to events and receive data published to those events. **Special JS Feature/Syntax** There are no special JavaScript features or syntax mentioned in this benchmark. **Other Considerations** When choosing between these two approaches, consider the specific requirements of your application: * If you need simple, lightweight event handling with minimal overhead, `Custom Event` might be a good choice. * If you require more complex event structures and optimized event handling, `PubSub Single Event 10` might be a better option. **Other Alternatives** Alternative approaches to pub/sub patterns include: 1. **Event Bus**: A library that provides an efficient and flexible way to handle events between components in web applications. 2. **RxJS**: A popular JavaScript library for reactive programming, which includes support for observables and event handling. In summary, this benchmark compares two event handling approaches: `Custom Event` and `PubSub Single Event 10`. The choice of approach depends on the specific requirements of your application, considering factors such as simplicity, performance, and complexity.
Related benchmarks:
momentjs isBefore versus lt operator
moment is before vs js
Date.now() vs Moment()
MomentJS vs Native test
moment with & without formats, vs moment new Date & moment date.parse
Comments
Confirm delete:
Do you really want to delete benchmark?