Explorer
JavaScript

Write a class or function `createEventEmitter()` that supports `on(event, callback)`, `emit(event, ...args)`, and `off(event, callback)` methods.

Problem Statement

<p>Write a class or function <code>createEventEmitter()</code> that supports <code>on(event, callback)</code>, <code>emit(event, ...args)</code>, and <code>off(event, callback)</code> methods.</p>

Examples

Input: const ee = createEventEmitter(); ee.on("event", fn); ee.emit("event", data);

Output: fn invoked with data

Explanation: Publisher-subscriber event dispatcher.

Complexity

Time Complexity: O(N)

Space Complexity: O(1)

Hints

šŸ’” Hint 1: Store listeners in a Map where keys are event names and values are arrays of callbacks. šŸ’” Hint 2: emit calls all callbacks registered for the event. šŸ’” Hint 3: off removes the specific callback from the event listeners array.

Editorial & Approach

Problem Overview & Intuition

To solve Event Emitter, we consider the execution characteristics of JavaScript engines. Write a class or function `createEventEmitter()` that supports `on(event, callback)`, `emit(event, ...args)`, and `off(event, callback)` methods. By utilizing idiomatic language constructs and clean algorithmic principles, we can accomplish this with optimal time and memory usage.

Step-by-Step Approach

  1. Understand Problem Contract: Identify input arguments, return type expectations, and edge cases (empty inputs, nullish values).
  2. Choose Core Mechanism: Use modern JavaScript patterns (store listeners in a map where keys are event names and values are arrays of callbacks).
  3. Implement Logic: Handle state and transformations efficiently (emit calls all callbacks registered for the event).
  4. Return Result: Ensure proper return format and preserve caller context if applicable.

Optimal Implementation (JavaScript)

function createEventEmitter() {
  const events = {};
  return {
    on(event, cb) {
      if (!events[event]) events[event] = [];
      events[event].push(cb);
    },
    emit(event, ...args) {
      (events[event] || []).forEach(cb => cb(...args));
    },
    off(event, cb) {
      if (events[event]) {
        events[event] = events[event].filter(fn => fn !== cb);
      }
    }
  };
}

Complexity Analysis

Time Complexity O(N) linear scan over input
Space Complexity O(1) constant auxiliary space (or O(N) output)

Edge Cases & Corner Traps Handled

  • Empty or boundary inputs (empty arrays, strings, zero length).
  • Type checks and unexpected values (e.g. null, undefined, negative numbers).
  • Closure preservation and memory isolation between separate invocations.

Event Emitter

Hard

Write a class or function createEventEmitter() that supports on(event, callback), emit(event, ...args), and off(event, callback) methods.

Example Scenarios
1Example 1
Input: const ee = createEventEmitter(); ee.on("event", fn); ee.emit("event", data);
Output: fn invoked with data
Explanation:

Publisher-subscriber event dispatcher.

Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
[1,2]