New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

tseep

Package Overview
Dependencies
Maintainers
0
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tseep

Fastest event emitter in the world

  • 1.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.7K
decreased by-14.55%
Maintainers
0
Weekly downloads
 
Created
Source

NPM Version GitHub stars

tseep

Because there are N fastest event emitters. And we are fastest (feb 2023) 😏.

Up to x12 faster than eventemitter3 in terms of "classic api event emitters" (currently fastest for not classic too).


  • Fully typed args of emit method based on events map
  • Fully implements NodeJS.EventEmitter type & standart, provides interface
  • Worlds fastest pure-js EventEmitter
  • Fully tested with eventemitter3 tests
  • No external deps
  • Only 381 bytes size in real app (brotlied)
  • No eval implemented

how it works

Benchmarks

emit-multiple-listeners:

tseep               89,030,882 ops/sec
tseep no-eval       15,235,353 ops/sec
emitix              6,201,874 ops/sec
fastemitter         5,981,406 ops/sec
EventEmitter3       5,698,255 ops/sec
tsee                5,163,550 ops/sec
EventEmitter2       4,588,433 ops/sec
EventEmitter1       4,437,743 ops/sec
mitt                3,587,734 ops/sec
event-emitter       3,508,490 ops/sec
contra/emitter      2,183,943 ops/sec

Fastest is [ 'tseep' ]

benchmarks

Make an issue to include yours event emitter, lets find the fastest!

Install & use

npm i tseep

Simple usage:

import { EventEmitter } from "tseep";

const events = new EventEmitter<{
    foo: (a: number, b: string) => void;
}>();

// foo's arguments is fully type checked
events.emit("foo", 123, "hello world");

Use no-eval version

There is no user code evaluated so its complete safe to use with-eval version.
But in some cases (eg chrome's extensions), you just cant use it.
For this cases there is no-eval version:

import { EventEmitter } from "tseep/lib/ee-safe"; // no-eval version
import { EventEmitter } from "tseep/lib/fallback"; // or with autofallback

// same api

"tseep/lib/fallback" may bundle both versions which may result in bigger app bundle size.

Api

EventEmitter<T> where T extends { [eventName]: Call signature }.

EventEmitter.emit's args is fully typed based on events map.

!! __proto__ event name is restricted (type guard exists) !!

By default listeners are not bound to EventEmitter, so you may get some problems around inheritance.
First of all, better use incapsulation. Its faster, safer, clear.
Other variant is to use addListenerBound/removeListenerBound.
Its 2-3x slower for add/remove operation but than you will have proper 'this' context inside listener.

// Listener = (...args: any[]) => Promise<any>|void
// EventMap extends { [event in (string|symbol)]: Listener }

class EventEmitter<EventMap> {
    readonly maxListeners: number;
    readonly _eventsCount: number;

    emit(event: EventKey, ...args: ArgsN<EventMap[EventKey]>): boolean;
    on(event: EventKey, listener: EventMap[EventKey]): this;
    once(event: EventKey, listener: EventMap[EventKey]): this;
    addListener(event: EventKey, listener: EventMap[EventKey], argsNum?: ArgsNum<EventMap[EventKey]>): this;
    removeListener(event: EventKey, listener: EventMap[EventKey]): this;
    hasListeners(event: EventKey): boolean;
    prependListener(event: EventKey, listener: EventMap[EventKey]): this;
    prependOnceListener(event: EventKey, listener: EventMap[EventKey]): this;
    off(event: EventKey, listener: EventMap[EventKey]): this;
    removeAllListeners(event?: EventKey): this;
    setMaxListeners(n: number): this;
    getMaxListeners(): number;
    listeners(event: EventKey): EventMap[EventKey][];
    rawListeners(event: EventKey): EventMap[EventKey][];
    eventNames(): Array<string | symbol>;
    listenerCount(type: EventKey): number;

    // special methods that are a bit slower than addListener/removeListener
    // but they binds listeners to current EventEmitter or custom object
    addListenerBound(event: EventKey, listener: EventMap[EventKey], bindTo?: any = this, argsNum?: ArgsNum<EventMap[EventKey]>): this;
    removeListenerBound(event: EventKey, listener: EventMap[EventKey]): this;
}

License

MIT

Keywords

FAQs

Package last updated on 26 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc