tseep极速事件发射器
tseep 号称是”世界上最快的事件发射器“,在“经典 API 事件发射器”方面,比 eventemitter3 快12倍。
- 基于事件映射的
emit
方法的全类型参数 - 完全实现
NodeJS.EventEmitter
类型和标准,提供接口 - 最快的pure-js
EventEmitter
- 使用 eventemitter 3测试进行全面测试
基准
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' ]
基准测试
安装和使用
npm i tseep
简单用法:
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>
,其中 T
延伸 { [eventName]: Call signature }
。
EventEmitter.emit
的args基于事件映射完全类型化。
!! __proto__
事件名称受限制(类型保护存在)!!
// 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;
}
评论