eventemitter3 vs mitt vs nanoevents
Event Handling Libraries Comparison
3 Years
eventemitter3mittnanoeventsSimilar Packages:
What's Event Handling Libraries?

Event handling libraries in JavaScript provide a way to implement the observer pattern, allowing one part of an application to notify other parts when something happens. This is useful for decoupling components, improving code organization, and enabling asynchronous communication within applications. These libraries offer APIs to create, manage, and trigger events, making it easier to build interactive and responsive applications. eventemitter3 is a fast and lightweight implementation of the EventEmitter class, mitt is a tiny and simple event emitter with a minimal API, and nanoevents is a super lightweight event emitter with a focus on performance and simplicity.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
eventemitter351,885,808
3,45473.4 kB182 years agoMIT
mitt11,512,097
11,51626.4 kB232 years agoMIT
nanoevents229,823
1,5835.43 kB010 months agoMIT
Feature Comparison: eventemitter3 vs mitt vs nanoevents

Size and Performance

  • eventemitter3:

    eventemitter3 is lightweight (about 1.5 KB minified) but offers more features than some minimalistic emitters. It strikes a good balance between functionality and performance, making it suitable for most applications without significant overhead.

  • mitt:

    mitt is extremely small (about 200 bytes minified) and provides a very fast event handling mechanism. Its minimalistic design ensures that it has a negligible impact on performance, making it ideal for high-performance applications where every byte and millisecond counts.

  • nanoevents:

    nanoevents is one of the smallest event emitter libraries (around 500 bytes minified). It is designed for high performance with low memory usage, making it an excellent choice for applications that require quick event handling without the overhead of larger libraries.

API Simplicity

  • eventemitter3:

    eventemitter3 provides a straightforward API for adding, removing, and emitting events. While it has more features than a basic emitter, its API is still easy to understand and use, making it accessible for developers of all skill levels.

  • mitt:

    mitt has a very simple and intuitive API that makes it easy to work with. It supports adding and removing listeners, as well as emitting events, all with minimal boilerplate code. Its simplicity is one of its main selling points.

  • nanoevents:

    nanoevents offers a clean and simple API for event handling. It allows for easy registration and deregistration of listeners, as well as emitting events. The API is designed to be intuitive, making it easy for developers to integrate into their projects.

Feature Set

  • eventemitter3:

    eventemitter3 supports multiple listeners, once listeners, and context binding, making it a more feature-rich solution compared to minimalistic emitters. It also provides built-in support for removing listeners, which adds to its flexibility and usability in more complex scenarios.

  • mitt:

    mitt is focused on simplicity and does not support multiple listeners for the same event by default. It allows for a single listener per event, which keeps the API clean but limits its functionality in scenarios where multiple listeners are needed.

  • nanoevents:

    nanoevents supports multiple listeners and provides a simple way to manage them. However, it does not have advanced features like once listeners or context binding, keeping it lightweight and easy to use while still offering enough functionality for most use cases.

Use Case Example

  • eventemitter3:

    Use eventemitter3 when you need a robust event system with support for multiple listeners, once listeners, and context. It is suitable for larger applications where you need more control over event handling and listener management.

  • mitt:

    Use mitt for simple event handling where you want to keep things lightweight and straightforward. It is ideal for small to medium projects where you don’t need complex event handling features.

  • nanoevents:

    Use nanoevents when you need a fast and lightweight event emitter with support for multiple listeners. It is great for performance-sensitive applications where you want to minimize memory usage and keep the event handling logic simple.

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');
    
  • 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');
    
  • nanoevents:

    nanoevents Example

    import { Nanoevents } from 'nanoevents';
    const emitter = new Nanoevents();
    
    // Add a listener
    const unsubscribe = emitter.on('event', () => console.log('Event triggered!'));
    
    // Emit the event
    emitter.emit('event');
    
    // Remove the listener
    unsubscribe();
    
How to Choose: eventemitter3 vs mitt vs nanoevents
  • eventemitter3:

    Choose eventemitter3 if you need a feature-rich and performant event emitter with support for multiple listeners, once listeners, and context binding. It is suitable for applications that require more advanced event handling capabilities while still being lightweight.

  • mitt:

    Choose mitt if you want a minimalistic and easy-to-use event emitter with a small footprint. It is ideal for projects where simplicity is key, and you don’t need advanced features like multiple listeners or context binding.

  • nanoevents:

    Choose nanoevents if you need an ultra-lightweight event emitter with a focus on performance and simplicity. It is perfect for applications where every byte counts, and you want a straightforward API for handling events without any unnecessary complexity.

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