简体   繁体   中英

Performance of custom events and observer pattern in Node.js

I have a dilemma. I can choose between:

  • Emiting custom events and reacting on them,
  • Implementing observer pattern.

Events looks to be more simple, but what about performance? Do you know about any performance tests of these two techniques? It would be very helpful.

//BTW

Is it possible to add scope for events?

Both eventing and observer pattern share the same principal: trigger and react. Since Java do not have function pointers, therefore, we use an object to wrap around a function and notify when an event is triggered. This is observer pattern.

JavaScript supports function pointers. So we can remove the object "wrapper" and keep a list of event handlers at its very raw form: a function.

I don't know any perf tests about observer pattern or Node.JS event emitter. But memory-wise, keeping things small helps perf. Moreover, when a event is triggered, eventing don't require you to use a member accessor (ie the dot). This should save you some perf too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM