eventemitter3 vs eventemitter2 vs mitt vs event-emitter vs tiny-emitter
Event Emitter Libraries Comparison
1 Year
eventemitter3eventemitter2mittevent-emittertiny-emitterSimilar Packages:
What's Event Emitter Libraries?

Event emitter libraries in JavaScript provide a way to implement the observer pattern, allowing objects to communicate with each other by emitting events and listening for them. These libraries facilitate asynchronous programming by enabling decoupled components to react to specific actions or changes in state, promoting a more modular and maintainable code structure. They are commonly used in Node.js and browser applications for handling events, managing state changes, and implementing custom event-driven architectures.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
eventemitter349,095,0333,43073.4 kB172 years agoMIT
eventemitter212,569,4152,88081.3 kB48-MIT
mitt10,161,50611,35526.4 kB202 years agoMIT
event-emitter7,588,564245-68 years agoMIT
tiny-emitter4,188,194957-96 years agoMIT
Feature Comparison: eventemitter3 vs eventemitter2 vs mitt vs event-emitter vs tiny-emitter

Size and Performance

  • eventemitter3:

    eventemitter3 is designed for high performance with a very small footprint, making it one of the fastest event emitters available, ideal for performance-critical applications.

  • eventemitter2:

    eventemitter2 is slightly larger than basic emitters but offers better performance for complex event handling, especially with its support for wildcard events and namespaces.

  • mitt:

    mitt is extremely small (just 200 bytes) and fast, making it one of the most lightweight event emitters available. Its simplicity ensures minimal performance impact, making it ideal for high-frequency event handling.

  • event-emitter:

    event-emitter is a lightweight library with minimal overhead, making it suitable for applications where performance is important but advanced features are not required.

  • tiny-emitter:

    tiny-emitter is a small library (about 1KB) that provides efficient event handling with low overhead. It is designed to be fast and lightweight, suitable for applications where performance is a concern.

Feature Set

  • eventemitter3:

    eventemitter3 focuses on providing a fast and efficient event handling system with a simple API. It supports multiple listeners, listener removal, and event emission, but does not include advanced features like namespaces or wildcards, keeping it lightweight and easy to use.

  • eventemitter2:

    eventemitter2 offers a rich feature set, including support for wildcard events, namespaces, and the ability to set maximum listeners to prevent memory leaks. This makes it more versatile for complex applications that require advanced event handling.

  • mitt:

    mitt provides a very simple API for emitting and listening to events. It supports multiple listeners per event and allows for easy removal of listeners. However, it does not include advanced features like namespaces, wildcards, or error handling, which keeps it minimal but limits its functionality for more complex use cases.

  • event-emitter:

    event-emitter provides basic event emitting and listening capabilities, allowing for simple one-to-one and one-to-many event handling. It does not include advanced features like namespaces or wildcard events, keeping the API simple and straightforward.

  • tiny-emitter:

    tiny-emitter supports multiple listeners, listener removal, and provides a straightforward API for event handling. It is simple and easy to use, but does not include advanced features like namespaces or wildcard events, making it suitable for projects that need basic event handling without complexity.

Ease of Use: Code Examples

  • eventemitter3:

    Basic usage of eventemitter3

    const EventEmitter = require('eventemitter3');
    const emitter = new EventEmitter();
    
    // Simple event handling
    emitter.on('event', () => console.log('Event triggered!'));
    emitter.emit('event');
    
  • eventemitter2:

    Basic usage of eventemitter2

    const EventEmitter2 = require('eventemitter2').EventEmitter2;
    const emitter = new EventEmitter2();
    
    // Wildcard event handling
    emitter.on('user.*', (data) => console.log('User event:', data));
    emitter.emit('user.login', { username: 'Alice' });
    
  • mitt:

    Basic usage of mitt

    const mitt = require('mitt');
    const emitter = mitt();
    
    // Simple event handling
    emitter.on('event', () => console.log('Event triggered!'));
    emitter.emit('event');
    
  • event-emitter:

    Basic usage of event-emitter

    const EventEmitter = require('event-emitter');
    const emitter = EventEmitter();
    
    // Simple event handling
    emitter.on('event', () => console.log('Event triggered!'));
    emitter.emit('event');
    
  • tiny-emitter:

    Basic usage of tiny-emitter

    const Emitter = require('tiny-emitter');
    const emitter = new Emitter();
    
    // Simple event handling
    emitter.on('event', () => console.log('Event triggered!'));
    emitter.emit('event');
    
How to Choose: eventemitter3 vs eventemitter2 vs mitt vs event-emitter vs tiny-emitter
  • eventemitter3:

    Choose eventemitter3 if you prioritize performance and a small footprint. It is designed to be one of the fastest event emitters available, making it a great choice for performance-critical applications where every millisecond counts, without sacrificing functionality.

  • eventemitter2:

    Choose eventemitter2 if you need a feature-rich event emitter with support for wildcard events, namespaces, and better performance for high-frequency events. It is ideal for larger applications that require more advanced event handling capabilities while still being relatively lightweight.

  • mitt:

    Choose mitt if you want an ultra-lightweight (just 200 bytes) and simple event emitter with a minimal API. It is perfect for projects where simplicity and low overhead are key, and you don’t need advanced features like namespaces or wildcard events.

  • event-emitter:

    Choose event-emitter if you need a simple and lightweight implementation of the event emitter pattern with a focus on minimalism and ease of use. It is suitable for small to medium-sized projects where basic event handling is required.

  • tiny-emitter:

    Choose tiny-emitter if you need a tiny (about 1KB) and straightforward event emitter with support for multiple listeners and the ability to remove them easily. It strikes a good balance between size and functionality, making it suitable for projects that need a reliable emitter without a lot of bloat.

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