EventEmitter3 focuses on performance while maintaining a Node.js AND browser compatible interface.
eventemitter3 is a high-performance implementation of the EventEmitter pattern that works identically in Node.js and browser environments. It provides the same API as Node.js's built-in events module but with significant performance optimizations and zero dependencies. At roughly 2-3KB minified, it's designed for scenarios where event-driven architecture is needed without the overhead of Node's native implementation.
The library exists to solve two key problems: performance bottlenecks in event-heavy applications and the need for consistent event handling across server and client code. While Node's built-in EventEmitter works well for most cases, applications that emit thousands of events per second or run in resource-constrained environments benefit from eventemitter3's micro-optimizations. The library achieves faster execution through streamlined code paths while maintaining full API compatibility.
With over 72 million weekly downloads, eventemitter3 is widely adopted in production applications, particularly in frameworks and libraries that need universal JavaScript support. It's used by developers building real-time applications, pub/sub systems, UI component libraries, and any architecture where decoupled components communicate through events. The library includes full TypeScript support with generics for type-safe event definitions.
Version 5.x maintains backward compatibility while adding modern features like better TypeScript definitions and continued performance improvements. The MIT license and active maintenance make it a reliable choice for both open-source projects and commercial applications.
import EventEmitter from 'eventemitter3';
interface Events {
'user:login': (userId: string, timestamp: Date) => void;
'data:received': (payload: { id: number; value: string }) => void;
'error': (error: Error) => void;
}
const emitter = new EventEmitter<Events>();
class UserSession {
constructor(private sessionId: string) {}
handleLogin(userId: string, timestamp: Date) {
console.log(`[${this.sessionId}] User ${userId} logged in at ${timestamp}`);
}
}
const session = new UserSession('session-123');
emitter.on('user:login', session.handleLogin, session);
emitter.once('data:received', (payload) => {
console.log('First data packet:', payload);
});
const errorHandler = (error: Error) => {
console.error('Error occurred:', error.message);
};
emitter.on('error', errorHandler);
emitter.emit('user:login', 'user-456', new Date());
emitter.emit('data:received', { id: 1, value: 'test' });
emitter.emit('data:received', { id: 2, value: 'ignored' });
console.log('Active events:', emitter.eventNames());
console.log('Error listeners:', emitter.listenerCount('error'));
emitter.removeListener('error', errorHandler);
emitter.removeAllListeners();Real-time data streaming applications: Use eventemitter3 to handle high-frequency data events in WebSocket connections, server-sent events, or IoT data pipelines where thousands of messages per second need efficient distribution to multiple listeners without creating performance bottlenecks.
Cross-platform component libraries: Build UI component systems or SDK libraries that work identically in Node.js and browsers, using eventemitter3 for lifecycle hooks, state change notifications, and custom events without platform-specific code or polyfills.
Plugin architectures: Implement extensible applications where core modules emit events that plugins can subscribe to, enabling loose coupling between system components. The context binding feature allows plugins to maintain their own scope while listening to global events.
Testing and mocking: Replace Node's built-in EventEmitter in test environments to gain consistent behavior across different Node versions and reduce test execution time through faster event processing, particularly useful in CI/CD pipelines with extensive event-driven test suites.
Game development and animation systems: Handle game loop events, collision detection, animation frame updates, and user input events where performance directly impacts frame rates and user experience, especially in browser-based games or interactive visualizations.
npm install eventemitter3pnpm add eventemitter3bun add eventemitter3