eventemitter3 vs events vs mitt vs pubsub-js
Event Handling Libraries Comparison
3 Years
eventemitter3eventsmittpubsub-jsSimilar Packages:
What's Event Handling Libraries?

Event handling libraries in JavaScript provide a way to create and manage events, allowing different parts of an application to communicate with each other in a decoupled manner. These libraries implement the Observer design pattern, where one or more listeners (observers) can subscribe to events emitted by an event emitter (subject). When an event is emitted, all subscribed listeners are notified, enabling asynchronous and reactive programming. This is particularly useful in scenarios like handling user interactions, managing state changes, or integrating with third-party APIs. Popular event handling libraries include eventemitter3, mitt, pubsub-js, and the built-in events module in Node.js.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
eventemitter352,079,369
3,45473.4 kB182 years agoMIT
events42,069,551
1,408-124 years agoMIT
mitt11,615,999
11,52026.4 kB232 years agoMIT
pubsub-js157,560
4,863113 kB2910 months agoMIT
Feature Comparison: eventemitter3 vs events vs mitt vs pubsub-js

Size and Performance

  • eventemitter3:

    eventemitter3 is a lightweight library (around 1KB minified) that provides high-performance event handling with minimal overhead. It is optimized for speed, making it suitable for applications where performance is critical.

  • events:

    The built-in events module in Node.js is efficient and well-optimized for server-side applications. However, its performance is comparable to other event emitters, and it may not be as lightweight as specialized libraries for client-side use.

  • mitt:

    mitt is an extremely lightweight event emitter (about 200 bytes minified) that offers fast performance with very little memory footprint. Its simplicity and minimalism make it ideal for performance-sensitive applications where every byte counts.

  • pubsub-js:

    pubsub-js is a lightweight library for publish-subscribe messaging, but it is slightly larger than mitt. Its performance is generally good, but it may introduce some overhead due to its topic-based architecture, which is a trade-off for its added functionality.

API Design

  • eventemitter3:

    eventemitter3 provides a simple and intuitive API for managing events. It supports adding, removing, and emitting events with multiple listeners, as well as once-only listeners. The API is well-documented and easy to use, making it accessible for developers of all skill levels.

  • events:

    The events module in Node.js offers a comprehensive API for event handling, including methods for adding, removing, and emitting listeners. It also supports event delegation and provides the EventEmitter class, which is a standard implementation used throughout the Node.js ecosystem.

  • mitt:

    mitt features a minimalistic API with just a few methods: on, off, and emit. Its simplicity makes it easy to learn and use, with no complex configurations or boilerplate code required. This makes it an excellent choice for developers who prefer a straightforward approach to event handling.

  • pubsub-js:

    pubsub-js provides a clear and structured API for publish-subscribe messaging. It allows for subscribing to topics, publishing messages to specific topics, and unsubscribing from topics. The API is intuitive and well-suited for applications that require a more organized approach to event handling.

Event Model

  • eventemitter3:

    eventemitter3 follows the traditional event emitter model, allowing multiple listeners to subscribe to the same event. It supports event removal and provides a mechanism for once-only listeners, which are called only once and then automatically removed. This model is flexible and allows for complex event handling scenarios.

  • events:

    The events module implements the classic event emitter model, where multiple listeners can be attached to an event. It supports event bubbling, delegation, and provides built-in support for error handling through the error event. This model is robust and well-suited for both simple and complex event-driven architectures.

  • mitt:

    mitt adopts a simple event model where multiple listeners can be attached to an event, but it does not support event removal or once-only listeners. This makes it easy to use but limits its flexibility for more complex event handling scenarios. It is best suited for lightweight applications where simplicity is key.

  • pubsub-js:

    pubsub-js uses a publish-subscribe model, where events are categorized into topics. Multiple subscribers can listen to the same topic, and messages are delivered to all subscribers of that topic. This model allows for more organized event handling and reduces coupling between publishers and subscribers.

Ease of Use: Code Examples

  • eventemitter3:

    eventemitter3 Example

    import EventEmitter from 'eventemitter3';
    const emitter = new EventEmitter();
    
    // Add a listener
    emitter.on('event', () => console.log('Event triggered!'));
    
    // Emit the event
    emitter.emit('event');
    
  • events:

    Node.js events Example

    const EventEmitter = require('events');
    const emitter = new EventEmitter();
    
    // Add a listener
    emitter.on('event', () => console.log('Event triggered!'));
    
    // Emit the event
    emitter.emit('event');
    
  • mitt:

    mitt Example

    import mitt from 'mitt';
    const emitter = mitt();
    
    // Add a listener
    emitter.on('event', () => console.log('Event triggered!'));
    
    // Emit the event
    emitter.emit('event');
    
  • pubsub-js:

    pubsub-js Example

    import { PubSub } from 'pubsub-js';
    
    // Subscribe to a topic
    const token = PubSub.subscribe('topic', (msg, data) => {
      console.log(`Received message: ${data}`);
    });
    
    // Publish a message to the topic
    PubSub.publish('topic', 'Hello, World!');
    
    // Unsubscribe from the topic
    PubSub.unsubscribe(token);
    
How to Choose: eventemitter3 vs events vs mitt vs pubsub-js
  • eventemitter3:

    Choose eventemitter3 if you need a fast, lightweight, and feature-rich event emitter with support for multiple listeners, once-only listeners, and event removal. It is ideal for performance-sensitive applications and provides a simple API for managing events.

  • events:

    Choose the built-in events module if you are working in a Node.js environment and need a reliable, battle-tested solution for event handling. It supports all standard event emitter features and is well-integrated with the Node.js ecosystem, making it suitable for server-side applications.

  • mitt:

    Choose mitt if you want a minimalistic and lightweight event emitter with a small API surface. It is perfect for projects where simplicity and ease of use are priorities, and you don’t need advanced features like namespaces or once-only listeners.

  • pubsub-js:

    Choose pubsub-js if you need a publish-subscribe implementation that supports topics and allows for more structured event handling. It is useful for applications that require a more organized approach to event management, with support for multiple subscribers per topic.

README for eventemitter3

EventEmitter3

Version npmCICoverage Status

Sauce Test Status

EventEmitter3 is a high performance EventEmitter. It has been micro-optimized for various of code paths making this, one of, if not the fastest EventEmitter available for Node.js and browsers. The module is API compatible with the EventEmitter that ships by default with Node.js but there are some slight differences:

  • Domain support has been removed.
  • We do not throw an error when you emit an error event and nobody is listening.
  • The newListener and removeListener events have been removed as they are useful only in some uncommon use-cases.
  • The setMaxListeners, getMaxListeners, prependListener and prependOnceListener methods are not available.
  • Support for custom context for events so there is no need to use fn.bind.
  • The removeListener method removes all matching listeners, not only the first.

It's a drop in replacement for existing EventEmitters, but just faster. Free performance, who wouldn't want that? The EventEmitter is written in EcmaScript 3 so it will work in the oldest browsers and node versions that you need to support.

Installation

$ npm install --save eventemitter3

CDN

Recommended CDN:

https://unpkg.com/eventemitter3@latest/dist/eventemitter3.umd.min.js

Usage

After installation the only thing you need to do is require the module:

var EventEmitter = require('eventemitter3');

And you're ready to create your own EventEmitter instances. For the API documentation, please follow the official Node.js documentation:

http://nodejs.org/api/events.html

Contextual emits

We've upgraded the API of the EventEmitter.on, EventEmitter.once and EventEmitter.removeListener to accept an extra argument which is the context or this value that should be set for the emitted events. This means you no longer have the overhead of an event that required fn.bind in order to get a custom this value.

var EE = new EventEmitter()
  , context = { foo: 'bar' };

function emitted() {
  console.log(this === context); // true
}

EE.once('event-name', emitted, context);
EE.on('another-event', emitted, context);
EE.removeListener('another-event', emitted, context);

Tests and benchmarks

This module is well tested. You can run:

  • npm test to run the tests under Node.js.
  • npm run test-browser to run the tests in real browsers via Sauce Labs.

We also have a set of benchmarks to compare EventEmitter3 with some available alternatives. To run the benchmarks run npm run benchmark.

Tests and benchmarks are not included in the npm package. If you want to play with them you have to clone the GitHub repository. Note that you will have to run an additional npm i in the benchmarks folder before npm run benchmark.

License

MIT