event-emitter vs eventemitter2 vs eventemitter3 vs mitt vs tiny-emitter
Event Emitter Libraries
event-emittereventemitter2eventemitter3mitttiny-emitterSimilar Packages:

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.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
event-emitter0244-69 years agoMIT
eventemitter202,93381.3 kB50-MIT
eventemitter303,53274.4 kB215 months agoMIT
mitt011,87226.4 kB263 years agoMIT
tiny-emitter0972-97 years agoMIT

Feature Comparison: event-emitter vs eventemitter2 vs eventemitter3 vs mitt vs tiny-emitter

Size and Performance

  • 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.

  • 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.

  • 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.

  • 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.

  • 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

  • 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.

  • 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.

  • 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.

  • 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.

  • 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

  • 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');
    
  • 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' });
    
  • 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');
    
  • 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');
    
  • 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: event-emitter vs eventemitter2 vs eventemitter3 vs mitt vs tiny-emitter

  • 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.

  • 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.

  • 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.

  • 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.

  • 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 event-emitter

event-emitter

Environment agnostic event emitter

Installation

$ npm install event-emitter

To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: Browserify, Webmake or Webpack

Usage

var ee = require('event-emitter');

var MyClass = function () { /* .. */ };
ee(MyClass.prototype); // All instances of MyClass will expose event-emitter interface

var emitter = new MyClass(), listener;

emitter.on('test', listener = function (args) {
  // … react to 'test' event
});

emitter.once('test', function (args) {
  // … react to first 'test' event (invoked only once!)
});

emitter.emit('test', arg1, arg2/*…args*/); // Two above listeners invoked
emitter.emit('test', arg1, arg2/*…args*/); // Only first listener invoked

emitter.off('test', listener);              // Removed first listener
emitter.emit('test', arg1, arg2/*…args*/); // No listeners invoked

Additional utilities

allOff(obj) (event-emitter/all-off)

Removes all listeners from given event emitter object

hasListeners(obj[, name]) (event-emitter/has-listeners)

Whether object has some listeners attached to the object. When name is provided, it checks listeners for specific event name

var emitter = ee();
var hasListeners = require('event-emitter/has-listeners');
var listener = function () {};

hasListeners(emitter); // false

emitter.on('foo', listener);
hasListeners(emitter); // true
hasListeners(emitter, 'foo'); // true
hasListeners(emitter, 'bar'); // false

emitter.off('foo', listener);
hasListeners(emitter, 'foo'); // false

pipe(source, target[, emitMethodName]) (event-emitter/pipe)

Pipes all events from source emitter onto target emitter (all events from source emitter will be emitted also on target emitter, but not other way).
Returns pipe object which exposes pipe.close function. Invoke it to close configured pipe.
It works internally by redefinition of emit method, if in your interface this method is referenced differently, provide its name (or symbol) with third argument.

unify(emitter1, emitter2) (event-emitter/unify)

Unifies event handling for two objects. Events emitted on emitter1 would be also emitted on emitter2, and other way back.
Non reversible.

var eeUnify = require('event-emitter/unify');

var emitter1 = ee(), listener1, listener3;
var emitter2 = ee(), listener2, listener4;

emitter1.on('test', listener1 = function () { });
emitter2.on('test', listener2 = function () { });

emitter1.emit('test'); // Invoked listener1
emitter2.emit('test'); // Invoked listener2

var unify = eeUnify(emitter1, emitter2);

emitter1.emit('test'); // Invoked listener1 and listener2
emitter2.emit('test'); // Invoked listener1 and listener2

emitter1.on('test', listener3 = function () { });
emitter2.on('test', listener4 = function () { });

emitter1.emit('test'); // Invoked listener1, listener2, listener3 and listener4
emitter2.emit('test'); // Invoked listener1, listener2, listener3 and listener4

Tests Build Status

$ npm test