GetStream / stream-chat-js

JS / Browser Client - Build Chat with GetStream.io
https://getstream.io/chat/
Other
183 stars 76 forks source link

QUESTION: Single listener vs. multiple listeners #1394

Closed jhwheeler closed 1 week ago

jhwheeler commented 1 week ago

Is it better to have multiple listeners on a channel, each one listening to a different event, or one listener that listens to all events and then filter w/ if statements? I have seen both patterns in the docs, and I want to know which is better for performance.

For example, here is some Svelte code:

function setupListeners() {
    listeners = [
      channel.on('message.new', (event) => {
        if (event.message) {
          messages = [...messages, event.message];
          channel.markRead();
        }
      }),

      channel.on('message.read', (event) => {
        if (event.last_read_message_id && event.user?.id === otherMember?.user?.id) {
          lastReadMessageId = event.last_read_message_id;
        }
      }),

      channel.on('message.deleted', (event) => {
        if (event.message) {
          messages = messages.map((message) =>
            message.id === event.message?.id ? event.message : message,
          );
        }
      }),
    ];
  }

  function clearListeners() {
    listeners.forEach((listener) => listener?.unsubscribe());
    listeners = [];
  }

  onDestroy(() => {
    clearListeners();
  });

Would it be better for performance to instead have this:

function setupListener() {
    listener = channel.on((event) => {
      if (event.message && event.type === 'message.new') {
        messages = [...messages, event.message];
        channel.markRead();
      }

      if (
        event.type === 'message.read' &&
        event.last_read_message_id &&
        event.user?.id === otherMember?.user?.id
      ) {
        lastReadMessageId = event.last_read_message_id;
      }

      if (event.type === 'message.deleted' && event.message) {
        messages = messages.map((message) =>
          message.id === event.message?.id ? event.message : message,
        );
      }
    });
  }

  function clearListener() {
    listener?.unsubscribe();
  }

  onDestroy(() => {
    clearListener();
  });

I asked GPT about this, and its concluding statement was:

While a single listener reduces memory usage slightly, the performance benefit of multiple listeners lies in avoiding unnecessary condition evaluations for every event and leveraging the optimized event dispatching mechanisms provided by the library. For most cases, the gain in clarity, separation of concerns, and slight performance improvement outweigh the minimal memory cost of having multiple listeners.

Is this accurate?

Thanks in advance!

szuperaz commented 1 week ago

Hi,

The Chat GPT answer is accurate, it's true for all kind of event dispatcher systems, not just the Stream JS client. But since performance gains are so minuscule, at the end it comes down to personal/team preference. So you can go with either approach.

jhwheeler commented 1 week ago

@szuperaz Great, thank you for the quick and clear response!