mitt vs nanoevents
Lightweight Event Emitters for Modern Frontend Architecture
mittnanoeventsSimilar Packages:

Lightweight Event Emitters for Modern Frontend Architecture

mitt and nanoevents are ultra-small, dependency-free event emitter libraries designed to replace the heavy EventEmitter found in Node.js or the verbose native EventTarget in browsers. They provide a simple API for publishing and subscribing to events, making them ideal for state management, component communication, and decoupling logic in modern applications. While both solve the same core problem with a tiny footprint, mitt focuses on a familiar Map-like API with wildcard support, whereas nanoevents prioritizes absolute minimalism and tree-shakable ES module design.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
mitt011,89726.4 kB273 years agoMIT
nanoevents01,6325.48 kB018 days agoMIT

Mitt vs Nanoevents: Choosing the Right Tiny Event Bus

In modern frontend development, we often need a way for different parts of our application to talk to each other without being tightly coupled. While browsers provide the native EventTarget API and Node.js offers EventEmitter, both can be too heavy or verbose for simple tasks like managing global UI state or tracking analytics. This is where mitt and nanoevents step in. They are both tiny, zero-dependency libraries that solve this problem, but they take slightly different approaches to API design and feature sets.

πŸ“‘ Core API: Map-Like vs. Function-Based

The most immediate difference you'll notice is how you create and use the emitter. mitt returns an object that looks and feels like a Map, while nanoevents returns a simple object with an on and emit method.

mitt uses a Map-like interface. You create an emitter and immediately have access to methods like on, off, and emit. It also exposes the internal storage if you need it, though direct manipulation is rarely required.

import mitt from 'mitt';

const emitter = mitt();

// Subscribe to an event
emitter.on('user:login', (user) => {
  console.log(`Welcome, ${user.name}`);
});

// Emit an event
emitter.emit('user:login', { name: 'Alice' });

// Unsubscribe (requires the exact same function reference)
const handler = (data) => console.log(data);
emitter.on('data:update', handler);
emitter.off('data:update', handler);

nanoevents is even more stripped down. It exports a class (or a function depending on how you import it) that you instantiate. The API is minimal: just on to listen and emit to trigger.

import { NanoEvents } from 'nanoevents';

const emitter = new NanoEvents();

// Subscribe to an event
emitter.on('user:login', (user) => {
  console.log(`Welcome, ${user.name}`);
});

// Emit an event
emitter.emit('user:login', { name: 'Alice' });

// Unsubscribe
const handler = (data) => console.log(data);
emitter.on('data:update', handler);
emitter.off('data:update', handler);

🌟 Wildcard Listeners: The Killer Feature

One of the biggest practical differences is support for wildcard events. If you are building a complex state store or an analytics system, you often want to listen to a category of events rather than listing every single one.

mitt supports wildcards out of the box. You can use the * character to listen to every event, or specific patterns if you structure your event names carefully (though it primarily supports a global * listener for all events).

import mitt from 'mitt';

const emitter = mitt();

// Listen to EVERY event
emitter.on('*', (type, data) => {
  console.log(`Event fired: ${type}`, data);
});

emitter.emit('user:login', { id: 1 }); // Logs: 

How to Choose: mitt vs nanoevents

  • mitt:

    Choose mitt if you need a robust, drop-in replacement for standard event emitters that supports wildcard listeners (e.g., listening to all 'user:*' events). It is the better choice for complex state management scenarios where you might need to remove specific handlers by reference or clear all listeners for a specific event type efficiently. Its API feels more like a traditional Map, making it intuitive for developers coming from Vue or other framework backgrounds.

  • nanoevents:

    Choose nanoevents if your primary constraint is bundle size and you only need basic publish/subscribe functionality without wildcards. It is perfect for simple communication channels, like notifying a UI of a background task completion, where the overhead of extra features is unnecessary. Its design encourages creating separate emitter instances for different concerns, which aligns well with modular, tree-shaken architectures.

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