mitt vs mitt vs nanoevents
Event Emitter Libraries Comparison
1 Year
mittmittnanoeventsSimilar Packages:
What's Event Emitter Libraries?

Event emitter libraries are lightweight tools that facilitate communication between different parts of an application by allowing components to emit and listen for events. They provide a simple and efficient way to handle asynchronous events, making it easier to build responsive and interactive applications. These libraries are particularly useful in scenarios where decoupling components is essential, as they promote a publish-subscribe pattern, enabling better organization and maintainability of code.

npm Package Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
mitt8,980,36211,07626.4 kB202 years agoMIT
mitt8,980,36211,07626.4 kB202 years agoMIT
nanoevents153,5821,5105.43 kB04 months agoMIT
Feature Comparison: mitt vs mitt vs nanoevents

Simplicity

  • mitt:

    Mitt is designed to be extremely simple and lightweight, with a minimal API that allows for quick implementation. It provides basic functionality for emitting and listening to events without any complex setup, making it easy for developers to get started quickly.

  • mitt:

    Mitt is designed to be extremely simple and lightweight, with a minimal API that allows for quick implementation. It provides basic functionality for emitting and listening to events without any complex setup, making it easy for developers to get started quickly.

  • nanoevents:

    Nanoevents, while still lightweight, offers a slightly more complex API that includes features like namespaces for events. This added structure can be beneficial for larger applications, but it may introduce a slight learning curve compared to Mitt.

Performance

  • mitt:

    Mitt is optimized for performance, with a focus on low overhead. Its minimalist design ensures that it can handle a high volume of events without significant impact on application performance, making it ideal for performance-sensitive applications.

  • mitt:

    Mitt is optimized for performance, with a focus on low overhead. Its minimalist design ensures that it can handle a high volume of events without significant impact on application performance, making it ideal for performance-sensitive applications.

  • nanoevents:

    Nanoevents is also designed for performance, but its additional features may introduce slight overhead compared to Mitt. However, it still maintains a high level of efficiency, making it suitable for applications that require more advanced event handling.

Extensibility

  • mitt:

    Mitt is not designed for extensibility; it provides a straightforward implementation that works well for most use cases without the need for additional features or customization.

  • mitt:

    Mitt is not designed for extensibility; it provides a straightforward implementation that works well for most use cases without the need for additional features or customization.

  • nanoevents:

    Nanoevents offers more extensibility options, allowing developers to create custom event handling solutions. Its support for namespaces can help organize events in larger applications, making it easier to manage and extend functionality.

Use Cases

  • mitt:

    Mitt is ideal for simple applications or components where you need a quick and efficient way to handle events without the need for complex event management. It works well in scenarios like small UI components or simple state management.

  • mitt:

    Mitt is ideal for simple applications or components where you need a quick and efficient way to handle events without the need for complex event management. It works well in scenarios like small UI components or simple state management.

  • nanoevents:

    Nanoevents is better suited for larger applications or when you need to manage multiple event types and namespaces. It is particularly useful in scenarios where you have complex interactions between components that require more structured event handling.

Community and Support

  • mitt:

    Mitt has a smaller community and fewer resources available compared to more established libraries, but its simplicity means that most developers can easily understand and implement it without extensive documentation.

  • mitt:

    Mitt has a smaller community and fewer resources available compared to more established libraries, but its simplicity means that most developers can easily understand and implement it without extensive documentation.

  • nanoevents:

    Nanoevents has a growing community and more resources available for developers. Its slightly more complex nature means that there are more discussions and examples available to help with implementation.

How to Choose: mitt vs mitt vs nanoevents
  • mitt:

    Choose Mitt for its simplicity and minimalistic design, especially when you need a straightforward event emitter without any additional overhead. It is ideal for small to medium-sized applications where performance and ease of use are priorities.

  • mitt:

    Choose Mitt for its simplicity and minimalistic design, especially when you need a straightforward event emitter without any additional overhead. It is ideal for small to medium-sized applications where performance and ease of use are priorities.

  • nanoevents:

    Choose Nanoevents if you require a more feature-rich event emitter with support for namespaces and a more structured approach to event handling. It is suitable for larger applications where you may need more control over event management.

README for mitt

mitt
npm build status gzip size

Mitt

Tiny 200b functional event emitter / pubsub.

  • Microscopic: weighs less than 200 bytes gzipped
  • Useful: a wildcard "*" event type listens to all events
  • Familiar: same names & ideas as Node's EventEmitter
  • Functional: methods don't rely on this
  • Great Name: somehow mitt wasn't taken

Mitt was made for the browser, but works in any JavaScript runtime. It has no dependencies and supports IE9+.

Table of Contents

Install

This project uses node and npm. Go check them out if you don't have them locally installed.

$ npm install --save mitt

Then with a module bundler like rollup or webpack, use as you would anything else:

// using ES6 modules
import mitt from 'mitt'

// using CommonJS modules
var mitt = require('mitt')

The UMD build is also available on unpkg:

<script src="https://unpkg.com/mitt/dist/mitt.umd.js"></script>

You can find the library on window.mitt.

Usage

import mitt from 'mitt'

const emitter = mitt()

// listen to an event
emitter.on('foo', e => console.log('foo', e) )

// listen to all events
emitter.on('*', (type, e) => console.log(type, e) )

// fire an event
emitter.emit('foo', { a: 'b' })

// clearing all events
emitter.all.clear()

// working with handler references:
function onFoo() {}
emitter.on('foo', onFoo)   // listen
emitter.off('foo', onFoo)  // unlisten

Typescript

Set "strict": true in your tsconfig.json to get improved type inference for mitt instance methods.

import mitt from 'mitt';

type Events = {
  foo: string;
  bar?: number;
};

const emitter = mitt<Events>(); // inferred as Emitter<Events>

emitter.on('foo', (e) => {}); // 'e' has inferred type 'string'

emitter.emit('foo', 42); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. (2345)

Alternatively, you can use the provided Emitter type:

import mitt, { Emitter } from 'mitt';

type Events = {
  foo: string;
  bar?: number;
};

const emitter: Emitter<Events> = mitt<Events>();

Examples & Demos

Preact + Mitt Codepen Demo
preact + mitt preview

API

Table of Contents

mitt

Mitt: Tiny (~200b) functional event emitter / pubsub.

Returns Mitt

all

A Map of event names to registered handler functions.

on

Register an event handler for the given type.

Parameters

  • type (string | symbol) Type of event to listen for, or '*' for all events
  • handler Function Function to call in response to given event

off

Remove an event handler for the given type. If handler is omitted, all handlers of the given type are removed.

Parameters

  • type (string | symbol) Type of event to unregister handler from, or '*'
  • handler Function? Handler function to remove

emit

Invoke all handlers for the given type. If present, '*' handlers are invoked after type-matched handlers.

Note: Manually firing '*' handlers is not supported.

Parameters

  • type (string | symbol) The event type to invoke
  • evt Any? Any value (object is recommended and powerful), passed to each handler

Contribute

First off, thanks for taking the time to contribute! Now, take a moment to be sure your contributions make sense to everyone else.

Reporting Issues

Found a problem? Want a new feature? First of all see if your issue or idea has already been reported. If don't, just open a new clear and descriptive issue.

Submitting pull requests

Pull requests are the greatest contributions, so be sure they are focused in scope, and do avoid unrelated commits.

  • Fork it!
  • Clone your fork: git clone https://github.com/<your-username>/mitt
  • Navigate to the newly cloned directory: cd mitt
  • Create a new branch for the new feature: git checkout -b my-new-feature
  • Install the tools necessary for development: npm install
  • Make your changes.
  • Commit your changes: git commit -am 'Add some feature'
  • Push to the branch: git push origin my-new-feature
  • Submit a pull request with full remarks documenting your changes.

License

MIT License © Jason Miller

README for mitt

mitt
npm build status gzip size

Mitt

Tiny 200b functional event emitter / pubsub.

  • Microscopic: weighs less than 200 bytes gzipped
  • Useful: a wildcard "*" event type listens to all events
  • Familiar: same names & ideas as Node's EventEmitter
  • Functional: methods don't rely on this
  • Great Name: somehow mitt wasn't taken

Mitt was made for the browser, but works in any JavaScript runtime. It has no dependencies and supports IE9+.

Table of Contents

Install

This project uses node and npm. Go check them out if you don't have them locally installed.

$ npm install --save mitt

Then with a module bundler like rollup or webpack, use as you would anything else:

// using ES6 modules
import mitt from 'mitt'

// using CommonJS modules
var mitt = require('mitt')

The UMD build is also available on unpkg:

<script src="https://unpkg.com/mitt/dist/mitt.umd.js"></script>

You can find the library on window.mitt.

Usage

import mitt from 'mitt'

const emitter = mitt()

// listen to an event
emitter.on('foo', e => console.log('foo', e) )

// listen to all events
emitter.on('*', (type, e) => console.log(type, e) )

// fire an event
emitter.emit('foo', { a: 'b' })

// clearing all events
emitter.all.clear()

// working with handler references:
function onFoo() {}
emitter.on('foo', onFoo)   // listen
emitter.off('foo', onFoo)  // unlisten

Typescript

Set "strict": true in your tsconfig.json to get improved type inference for mitt instance methods.

import mitt from 'mitt';

type Events = {
  foo: string;
  bar?: number;
};

const emitter = mitt<Events>(); // inferred as Emitter<Events>

emitter.on('foo', (e) => {}); // 'e' has inferred type 'string'

emitter.emit('foo', 42); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. (2345)

Alternatively, you can use the provided Emitter type:

import mitt, { Emitter } from 'mitt';

type Events = {
  foo: string;
  bar?: number;
};

const emitter: Emitter<Events> = mitt<Events>();

Examples & Demos

Preact + Mitt Codepen Demo
preact + mitt preview

API

Table of Contents

mitt

Mitt: Tiny (~200b) functional event emitter / pubsub.

Returns Mitt

all

A Map of event names to registered handler functions.

on

Register an event handler for the given type.

Parameters

  • type (string | symbol) Type of event to listen for, or '*' for all events
  • handler Function Function to call in response to given event

off

Remove an event handler for the given type. If handler is omitted, all handlers of the given type are removed.

Parameters

  • type (string | symbol) Type of event to unregister handler from, or '*'
  • handler Function? Handler function to remove

emit

Invoke all handlers for the given type. If present, '*' handlers are invoked after type-matched handlers.

Note: Manually firing '*' handlers is not supported.

Parameters

  • type (string | symbol) The event type to invoke
  • evt Any? Any value (object is recommended and powerful), passed to each handler

Contribute

First off, thanks for taking the time to contribute! Now, take a moment to be sure your contributions make sense to everyone else.

Reporting Issues

Found a problem? Want a new feature? First of all see if your issue or idea has already been reported. If don't, just open a new clear and descriptive issue.

Submitting pull requests

Pull requests are the greatest contributions, so be sure they are focused in scope, and do avoid unrelated commits.

  • Fork it!
  • Clone your fork: git clone https://github.com/<your-username>/mitt
  • Navigate to the newly cloned directory: cd mitt
  • Create a new branch for the new feature: git checkout -b my-new-feature
  • Install the tools necessary for development: npm install
  • Make your changes.
  • Commit your changes: git commit -am 'Add some feature'
  • Push to the branch: git push origin my-new-feature
  • Submit a pull request with full remarks documenting your changes.

License

MIT License © Jason Miller