Skip to content

Commit

Permalink
events: optimize various functions
Browse files Browse the repository at this point in the history
Cache events and listeners objects where possible and loop over
Object.keys() instead of using for..in. These changes alone give
~60-65% improvement in the ee-add-remove benchmark.

The changes to EventEmitter.listenerCount() gives ~14%
improvement and changes to emitter.listeners() gives
significant improvements for <50 listeners
(~195% improvement for 10 listeners).

The changes to emitter.emit() gives 3x speedup for the fast
cases with multiple handlers and a minor speedup for the slow
case with multiple handlers.

The swapping out of the util.is* type checking functions with inline
checks gives another ~5-10% improvement.

PR-URL: #601
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Evan Lucas <evanlucas@me.com>
  • Loading branch information
mscdex authored and bnoordhuis committed Feb 9, 2015
1 parent c86e383 commit b677b84
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 98 deletions.
Loading

2 comments on commit b677b84

@tsbehlman
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for...in is much faster for objects as plain as the events object. The Object.keys() method is only faster if you would otherwise need to rely on hasOwnProperty. See this jsPerf. I can back this up with a benchmark suite running in io.js 1.1.0.

Update: I just pulled the changes and compared the added use of Object.keys to an equivalent for...in and found no significant difference in performance as reported by the events benchmarks.

@mscdex
Copy link
Contributor Author

@mscdex mscdex commented on b677b84 Feb 10, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tsbehlman See the discussion about that in the original PR here.

Please sign in to comment.