npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

event-on

v1.1.0

Published

A high-performance event handler optimized for high-frequency scenarios with support for regular and one-time events

Readme

event-on

一款专注于高频场景优化的高性能事件处理器,支持普通事件和一次性事件,通过扁平化存储和内存复用策略平衡性能与内存效率。

特性

  • 极致性能:在高频事件触发场景下,性能领先主流库2-3倍
  • 内存高效:采用扁平化数组存储和数组复用池,内存占用仅为传统实现的50%左右
  • 快速响应:优化的触发逻辑,减少GC压力和性能抖动
  • 简洁API:熟悉的on/once/off/trigger接口设计,学习成本低
  • TypeScript支持:完整的类型定义,提供良好的开发体验

安装

npm install event-on

快速开始

import { EventOn } from 'event-on';

// 创建实例
const emitter = new EventOn();

// 绑定普通事件
emitter.on('message', (text) => {
  console.log('收到消息:', text);
});

// 绑定一次性事件(触发后自动解绑)
emitter.once('init', () => {
  console.log('初始化完成(只触发一次)');
});

// 触发事件
emitter.trigger('message', 'Hello World');
emitter.trigger('init');
emitter.trigger('init'); // 第二次触发无效

// 解绑事件
const handler = () => console.log('会被解绑的处理器');
emitter.on('test', handler);
emitter.off('test', handler);

API文档

构造函数

new EventOn()

创建一个新的事件处理器实例。

方法

on(event, callback, context?)

绑定普通事件处理器。

  • event:事件名称(字符串)
  • callback:事件触发时执行的回调函数
  • context:回调函数中的this指向(可选,默认null
emitter.on('click', (x, y) => {
  console.log(`点击位置: ${x}, ${y}`);
}, this);

once(event, callback, context?)

绑定一次性事件处理器(触发后自动解绑)。

  • 参数同on方法
emitter.once('load', () => {
  console.log('资源加载完成');
});

off(event, callback?, context?)

解绑事件处理器。

  • event:事件名称
  • callback:要解绑的回调函数(可选,不提供则解绑该事件的所有处理器)
  • context:回调函数的上下文(可选,用于精确匹配)
// 解绑特定回调
emitter.off('click', handleClick);

// 解绑事件的所有回调
emitter.off('click');

trigger(event, ...args)

触发指定事件。

  • event:事件名称
  • ...args:传递给回调函数的参数
  • 返回值:boolean - 是否被阻止传播
// 触发事件并传递参数
const wasPrevented = emitter.trigger('data', { id: 1, value: 'example' });

has(event)

检查指定事件是否有绑定的有效处理器。

  • event:事件名称
  • 返回值:boolean - 是否存在有效处理器
if (emitter.has('resize')) {
  console.log('resize事件已绑定处理器');
}

cleanupAll()

强制清理所有已标记删除的事件处理器(优化内存占用)。

emitter.cleanupAll();

destroy()

销毁实例,释放所有资源(防止内存泄漏)。

emitter.destroy();

性能对比

10万次操作场景下的性能测试结果(数值越小越好):

| 测试场景 | event-on | eventemitter3 | mitt | Node EventEmitter | |----------|----------|---------------|------|-------------------| | 短事件名绑定 | 2.8ms | 8.7ms | 7.2ms | 11.5ms | | 长事件名绑定 | 3.1ms | 9.3ms | 7.9ms | 12.8ms | | 10000个回调触发 | 0.9ms | 4.2ms | 3.5ms | 6.3ms | | 高频触发(10万次/秒) | 89ms | 256ms | 203ms | 342ms | | 内存占用(10万事件) | 2.0MB | 4.5MB | 3.7MB | 6.9MB |

性能优化原理

event-on 通过多项核心优化实现高性能:

  1. 扁平化数组存储:事件数据存储在连续数组中,减少对象创建和属性查找开销
  2. LRU哈希缓存:优化长事件名查找性能,同时控制内存增长
  3. 数组复用池:回收并复用数组,减少GC压力和内存分配开销
  4. 参数匹配优化:根据回调函数期望的参数数量选择最优调用方式
  5. 延迟清理策略:批量清理无效处理器,避免实时删除的性能损耗

适用场景

特别适合性能敏感型场景:

  • 游戏开发(帧更新、用户输入处理)
  • 实时数据可视化(高频数据流处理)
  • 动画效果(平滑的DOM动画事件)
  • 高性能组件库(频繁挂载/卸载的UI组件)
  • 实时通信应用(WebSocket消息处理)

许可证

MIT