eventemitter3, mitt, and pubsub-js are libraries used to manage event communication within JavaScript applications. eventemitter3 offers a Node.js-like EventEmitter API optimized for browsers, providing a familiar interface for backend developers. mitt is a tiny 200-byte alternative focused on simplicity and modern frontend needs, stripping away legacy features. pubsub-js provides a global static API for publish-subscribe messaging, decoupling senders and receivers without requiring instance management.
When building interactive web applications, components need to talk to each other without creating tight dependencies. eventemitter3, mitt, and pubsub-js all solve this problem, but they take different approaches to API design and scope. Let's compare how they handle common engineering tasks.
The biggest difference lies in how you create and use the event system.
eventemitter3 requires you to create an instance for each event source.
// eventemitter3: Instance-based
import EventEmitter from 'eventemitter3';
const emitter = new EventEmitter();
emitter.on('update', (data) => {
console.log(data);
});
emitter.emit('update', { id: 1 });
mitt also uses an instance-based approach.
eventemitter3 but with a smaller footprint.// mitt: Instance-based
import mitt from 'mitt';
const emitter = mitt();
emitter.on('update', (data) => {
console.log(data);
});
emitter.emit('update', { id: 1 });
pubsub-js uses a global static API.
// pubsub-js: Global static
import PubSub from 'pubsub-js';
PubSub.subscribe('update', (msg, data) => {
console.log(data);
});
PubSub.publish('update', { id: 1 });
Not all libraries support the same level of control.
eventemitter3 includes advanced tools like once and removeAllListeners.
once listens for a single event then removes itself.removeAllListeners clears every handler for a topic.// eventemitter3: Advanced features
emitter.once('login', () => {
// Runs only once
});
emitter.removeAllListeners('login');
mitt keeps things minimal.
on, off, and emit.// mitt: Basic features only
const handler = (data) => {
emitter.off('update', handler);
console.log(data);
};
emitter.on('update', handler);
pubsub-js focuses on subscription management.
// pubsub-js: Token-based unsubscribe
const token = PubSub.subscribe('update', (msg, data) => {
console.log(data);
});
PubSub.unsubscribe(token);
How you organize events affects code maintenance.
eventemitter3 encourages isolation.
// eventemitter3: Passing instances
function Component({ emitter }) {
emitter.on('click', handleClick);
}
mitt also supports isolation.
// mitt: Multiple buses
const uiBus = mitt();
const dataBus = mitt();
uiBus.on('hover', handleHover);
dataBus.on('load', handleLoad);
pubsub-js uses a global scope.
// pubsub-js: Global scope
// Any file can import and publish
PubSub.publish('global-event', payload);
TypeScript support varies between these tools.
eventemitter3 has strong TypeScript definitions.
// eventemitter3: Typed events
interface Events {
update: { id: number };
}
const emitter = new EventEmitter<Events>();
mitt is built with TypeScript in mind.
// mitt: Typed events
import mitt from 'mitt';
type Events = {
update: { id: number };
};
const emitter = mitt<Events>();
pubsub-js has weaker type support.
// pubsub-js: Less strict typing
PubSub.subscribe('update', (msg, data: any) => {
// Data type is often loosely defined
});
You need to pass events between nested components without prop drilling.
mitt// mitt: Component bus
const bus = mitt();
// Pass bus via context or props
You need to track listeners, remove them all on cleanup, or listen once.
eventemitter3once and removeAllListeners simplify cleanup logic.// eventemitter3: Cleanup
emitter.removeAllListeners();
You have independent modules that should not know about each other.
pubsub-js// pubsub-js: Decoupled
// Module A publishes
PubSub.publish('data-ready', data);
// Module B subscribes independently
| Feature | eventemitter3 | mitt | pubsub-js |
|---|---|---|---|
| API Style | Instance | Instance | Global Static |
| Bundle Size | Small | Tiny | Small |
| Once Support | β Built-in | β Manual | β οΈ Varies |
| Type Safety | β Strong | β Strong | β οΈ Weak |
| Isolation | β High | β High | β Low |
Think about scope and complexity.
eventemitter3 (feature-rich) or mitt (minimal).pubsub-js.eventemitter3.These tools help keep your code organized β choose the one that matches your project's structure.
Choose eventemitter3 if you need a robust, Node.js-compatible event API with features like once and removeAllListeners in a browser environment. It is ideal for complex applications where you need multiple isolated emitters and strong typing support.
Choose mitt if bundle size is critical and you only need basic on/off/emit functionality without legacy baggage. It works best for simple state buses or component communication where you do not need advanced event features.
Choose pubsub-js if you prefer a global static API for loose coupling and do not need to manage multiple emitter instances. It suits projects where a central message hub is preferred over passing emitter objects around.
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:
throw an error when you emit an error event and nobody is
listening.newListener and removeListener events have been removed as they
are useful only in some uncommon use-cases.setMaxListeners, getMaxListeners, prependListener and
prependOnceListener methods are not available.fn.bind.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.
$ npm install --save eventemitter3
Recommended CDN:
https://unpkg.com/eventemitter3@latest/dist/eventemitter3.umd.min.js
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
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);
To run tests run npm test. 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.