Skip to the solution
MediumLow-Level Design6 min read

Explain how the EventEmitter class works under the hood.

967 views

The problem

Question presented to candidate: "Beyond 'on() subscribes, emit() calls listeners' β€” what does EventEmitter actually store internally, and does it treat one listener the same way as ten?"

What a strong answer should cover:

  • Internally, an EventEmitter instance keeps its listeners in a single property, _events β€” a plain object mapping event names to listener(s), created with Object.create(null) (a null-prototype object) specifically to avoid collisions with inherited properties like toString or constructor if an event happened to be named that.
  • πŸ“Œ A real, verifiable optimization: for an event with exactly one listener, _events[eventName] stores the bare function directly, not wrapped in an array. Only when a second listener is added for the same event does that slot convert into a genuine array β€” confirmed directly, not merely documented, by inspecting _events before and after adding a second listener.
  • .emit(event, ...args) looks up _events[event], and if it exists, calls it (or iterates the array and calls each) synchronously, in order β€” this is the same mechanism underlying the listener-ordering and .once()-removal behavior verified in the dedicated EventEmitter-basics question, now traced to its actual internal storage.
  • .once(event, listener) is implemented as a thin wrapper: internally, it registers a listener that, on its first invocation, removes itself (via the emitter's own .removeListener) before calling the original callback β€” it is not a separate internal mechanism from .on(), just .on() plus automatic self-removal logic layered on top.
  • The special-cased 'error' event behavior (verified with a real thrown exception in the dedicated EventEmitter-basics question) is implemented as an explicit check inside .emit() itself: if the event name is exactly 'error' and _events.error has no listener, .emit() throws the error argument directly rather than silently returning false the way it would for any other unlistened event.
  • A precise answer connects this to performance: the single-listener bare-function optimization avoids allocating an array for the extremely common case of exactly one listener per event, which matters given how pervasively EventEmitter underlies Streams, sockets, and HTTP internals throughout Node.

Clarifying questions expected:

  • "Is the interviewer asking about the public API (covered in the dedicated basics question) or genuinely the internal storage mechanism?" β€” this question is specifically about the latter.
  • "Does the answer need to address performance characteristics, or just correctness of the mechanism?"

Code / implementation expected: Yes β€” directly inspecting _events before and after adding a second listener, showing the bare-function-to-array conversion, is the concrete, convincing demonstration of genuine internal understanding.

  • eventemitter
  • patterns
  • events

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 9 sections

Read the code

JavaScriptInspecting EventEmitter's internal _events storage directly β€” bare function for one listener, real array for two
const EventEmitter = require("events");
const e = new EventEmitter();

console.log(e._events); // [Object: null prototype] {}

e.on("a", () => {});
console.log(e._events); // [Object: null prototype] { a: [Function (anonymous)] }
// -- a BARE function, not an array, for exactly one listener

e.on("a", () => {});
console.log(Array.isArray(e._events.a), e._events.a.length);
// true 2  -- NOW it is a real array, only after the second listener

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 53 of 152 in Node.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Node.js questions
Up nextMedium

How does Node.js handle crypto operations efficiently without blocking the event loop?

Continue