mitt vs nanoevents
イベントエミッタ
mittnanoevents類似パッケージ:

イベントエミッタ

イベントエミッタは、オブジェクトがイベントを発生させ、他のオブジェクトがそれに応じて反応できるようにするデザインパターンです。これにより、オブジェクト間の非同期通信が可能になり、柔軟で再利用可能なコードを構築できます。mittはシンプルで軽量なイベントエミッタライブラリで、少ないコードで迅速にイベントを管理できます。一方、nanoeventsはさらに小型で、パフォーマンスを重視したイベントエミッタで、特に高頻度のイベント処理に適しています。

npmのダウンロードトレンド

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
mitt011,86926.4 kB263年前MIT
nanoevents01,6275.43 kB02年前MIT

機能比較: mitt vs nanoevents

サイズとパフォーマンス

  • mitt:

    mittは非常に軽量で、ミニファイされた状態で約200バイトです。これにより、バンドルサイズが小さく、ウェブアプリケーションに簡単に統合できます。

  • nanoevents:

    nanoeventsはさらに小型で、ミニファイされた状態で約100バイトです。これにより、特にパフォーマンスが重要なアプリケーションや、リソースが限られた環境での使用に最適です。

APIのシンプルさ

  • mitt:

    mittは非常にシンプルなAPIを提供しており、イベントの発火、リッスン、解除が直感的に行えます。特別な設定や複雑な構成は不要で、すぐに使い始めることができます。

  • nanoevents:

    nanoeventsもシンプルなAPIを提供していますが、特にパフォーマンスを重視した設計になっています。イベントのリッスンや発火が迅速に行えるため、高頻度のイベント処理に適しています。

メモリ使用量

  • mitt:

    mittはメモリ使用量が少なく、イベントリスナーを管理するためのオーバーヘッドが最小限です。これにより、リソースを効率的に使用しながら、イベント駆動型のアプリケーションを構築できます。

  • nanoevents:

    nanoeventsはさらにメモリ効率が良く、特に大量のイベントリスナーを管理する際に優れたパフォーマンスを発揮します。リソースが限られた環境でも安定して動作します。

イベントリスナーの解除

  • mitt:

    mittでは、イベントリスナーを解除するための明示的なメソッドは提供されていませんが、リスナー関数を再利用することで、解除を実装できます。

  • nanoevents:

    nanoeventsは、リスナーを解除するための明示的なメソッドを提供しており、リスナーの管理が容易です。これにより、不要なリスナーを簡単に解除でき、メモリリークを防ぐことができます。

コード例

  • mitt:

    mittを使用したイベントエミッタの例

    import mitt from 'mitt';
    const emitter = mitt();
    
    // イベントリスナーの追加
    emitter.on('event', (data) => {
      console.log('イベント発生:', data);
    });
    
    // イベントの発火
    emitter.emit('event', { message: 'こんにちは、世界!' });
    
  • nanoevents:

    nanoeventsを使用したイベントエミッタの例

    import { Nanoevents } from 'nanoevents';
    const emitter = new Nanoevents();
    
    // イベントリスナーの追加
    const unsubscribe = emitter.on('event', (data) => {
      console.log('イベント発生:', data);
    });
    
    // イベントの発火
    emitter.emit('event', { message: 'こんにちは、世界!' });
    
    // イベントリスナーの解除
    unsubscribe();
    

選び方: mitt vs nanoevents

  • mitt:

    mittを選択する理由は、シンプルさと使いやすさです。少ないコードでイベントを発火させたり、リッスンしたりできるため、迅速なプロトタイピングや小規模なプロジェクトに最適です。

  • nanoevents:

    nanoeventsを選択する理由は、さらに小さく、パフォーマンスに優れている点です。特に、リソースが限られた環境や、高頻度でイベントを処理する必要があるアプリケーションに適しています。

mitt のREADME

mitt
npm build status gzip size

Mitt

Tiny 200b functional event emitter / pubsub.

  • Microscopic: weighs less than 200 bytes gzipped
  • Useful: a wildcard "*" event type listens to all events
  • Familiar: same names & ideas as Node's EventEmitter
  • Functional: methods don't rely on this
  • Great Name: somehow mitt wasn't taken

Mitt was made for the browser, but works in any JavaScript runtime. It has no dependencies and supports IE9+.

Table of Contents

Install

This project uses node and npm. Go check them out if you don't have them locally installed.

$ npm install --save mitt

Then with a module bundler like rollup or webpack, use as you would anything else:

// using ES6 modules
import mitt from 'mitt'

// using CommonJS modules
var mitt = require('mitt')

The UMD build is also available on unpkg:

<script src="https://unpkg.com/mitt/dist/mitt.umd.js"></script>

You can find the library on window.mitt.

Usage

import mitt from 'mitt'

const emitter = mitt()

// listen to an event
emitter.on('foo', e => console.log('foo', e) )

// listen to all events
emitter.on('*', (type, e) => console.log(type, e) )

// fire an event
emitter.emit('foo', { a: 'b' })

// clearing all events
emitter.all.clear()

// working with handler references:
function onFoo() {}
emitter.on('foo', onFoo)   // listen
emitter.off('foo', onFoo)  // unlisten

Typescript

Set "strict": true in your tsconfig.json to get improved type inference for mitt instance methods.

import mitt from 'mitt';

type Events = {
  foo: string;
  bar?: number;
};

const emitter = mitt<Events>(); // inferred as Emitter<Events>

emitter.on('foo', (e) => {}); // 'e' has inferred type 'string'

emitter.emit('foo', 42); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. (2345)

Alternatively, you can use the provided Emitter type:

import mitt, { Emitter } from 'mitt';

type Events = {
  foo: string;
  bar?: number;
};

const emitter: Emitter<Events> = mitt<Events>();

Examples & Demos

Preact + Mitt Codepen Demo
preact + mitt preview

API

Table of Contents

mitt

Mitt: Tiny (~200b) functional event emitter / pubsub.

Returns Mitt

all

A Map of event names to registered handler functions.

on

Register an event handler for the given type.

Parameters

  • type (string | symbol) Type of event to listen for, or '*' for all events
  • handler Function Function to call in response to given event

off

Remove an event handler for the given type. If handler is omitted, all handlers of the given type are removed.

Parameters

  • type (string | symbol) Type of event to unregister handler from, or '*'
  • handler Function? Handler function to remove

emit

Invoke all handlers for the given type. If present, '*' handlers are invoked after type-matched handlers.

Note: Manually firing '*' handlers is not supported.

Parameters

  • type (string | symbol) The event type to invoke
  • evt Any? Any value (object is recommended and powerful), passed to each handler

Contribute

First off, thanks for taking the time to contribute! Now, take a moment to be sure your contributions make sense to everyone else.

Reporting Issues

Found a problem? Want a new feature? First of all see if your issue or idea has already been reported. If don't, just open a new clear and descriptive issue.

Submitting pull requests

Pull requests are the greatest contributions, so be sure they are focused in scope, and do avoid unrelated commits.

  • Fork it!
  • Clone your fork: git clone https://github.com/<your-username>/mitt
  • Navigate to the newly cloned directory: cd mitt
  • Create a new branch for the new feature: git checkout -b my-new-feature
  • Install the tools necessary for development: npm install
  • Make your changes.
  • Commit your changes: git commit -am 'Add some feature'
  • Push to the branch: git push origin my-new-feature
  • Submit a pull request with full remarks documenting your changes.

License

MIT License © Jason Miller