Coder Social home page Coder Social logo

morglod / tseep Goto Github PK

View Code? Open in Web Editor NEW
152.0 3.0 4.0 454 KB

Fastest event emitter in the world for js

Home Page: https://github.com/Morglod/tseep/blob/master/benchmarks/README.md

License: MIT License

TypeScript 57.69% JavaScript 42.11% Shell 0.20%
emitter events fast javascript optimization typescript event-emitter event-listener eventemitter eventemitter3

tseep's Introduction

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

how it works

Benchmarks

emit-multiple-listeners:

tseep x            40,569,711 ops/sec  <---
EventEmitter1 x     4,498,223 ops/sec
EventEmitter2 x     4,536,296 ops/sec
EventEmitter3 x     5,852,395 ops/sec
fastemitter x       6,127,215 ops/sec
event-emitter x     3,449,595 ops/sec
contra/emitter x    2,186,002 ops/sec
tsee x              5,231,167 ops/sec
emitix x            6,549,983 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");

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

tseep's People

Contributors

morglod avatar nick-w-nick avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

tseep's Issues

Namespace based multi-level wildcard support

Hello! I like your efforts, and I am very interested in this package. I am developing a framework like structure and was looking for a performant event emitter which works on browser, node.js to use it as a part of the event manager. Do you consider supporting namespace wildcards?

Example implementation:

import { EventEmitter } from 'tseep';

const events = new EventEmitter();

// Assumed namespace separator is ":"
events.emit("foo:*:bar", "hello world");

events.on("foo:hello:bar", (arg) =>
{
  console.log(arg);
});

As another question, is it also possible to order the execution of events, like async support or something like that?

Thanks!

`prependOnceListener` does not work as expected

Hello,

I am having an issue about once listeners. Seems like prependOnceListener overrides all the once events. In the below example, it never logs "firstOnce" if I set prependOnceListener within once.

const ee = new EventEmitter();

ee.once('onceTest', () => { console.log('firstOnce') });

// ee.once('onceTest', () => { console.log('secondOnce') });
// ee.prependOnceListener('onceTest', () => { console.log('prependOnce') });

ee.prependOnceListener('onceTest', () => { console.log('prependOnce again') });

ee.emit('onceTest', 'yoo!');

The other problem is I cannot set multiple once events if I set prependOnceListener. If I uncomment them it's going to cause an error(FIXME) that you had pointed https://github.com/Morglod/tseep/blob/master/src/ee.ts#L216 here.

Mustafa,
Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.