Skip to content

events: optimize eventEmitter.once()

This basically removes the extra closure previously created by once() and instead uses an object to denote a one-time handler. The result is ~340% performance increase when adding one-time handlers, ~48% performance increase when emitting events with mixed persistent and one-time handlers, and ~10% performance increase when emitting with multiple arguments with mixed handlers, all with negligible negative performance impact to other EventEmitter functionality.

Also, removeListener() was optimized a bit by eliminating handler arrays of length 1 so that the leftover handler is set on _events[type].

The benchmarks from #912 were used when measuring the changes of this PR.

Merge request reports

Loading