Permalink
Browse files

if EventEmitter exposed a protected HaveListeners method then objects…

… that emit events with payloads which are particularly costly to construct could optimize by using it. This is a naive implementation that affords a 15% performance improvement in one sample use case. This patch would be more interesting if the HaveListeners check itself could be made less expensive
  • Loading branch information...
1 parent 8a03cf7 commit 1c99451b1aed866a853294f5d255996b3d818f53 @lloyd committed Apr 29, 2011
Showing with 12 additions and 0 deletions.
  1. +11 −0 src/node_events.cc
  2. +1 −0 src/node_events.h
View
@@ -89,4 +89,15 @@ bool EventEmitter::Emit(Handle<String> event, int argc, Handle<Value> argv[]) {
return true;
}
+bool EventEmitter::HaveListeners(Handle<String> event) {
+ HandleScope scope;
+ // HandleScope not needed here because only called from one of the two
+ // functions below
+ Local<Value> events_v = handle_->Get(events_symbol);
+ if (!events_v->IsObject()) return false;
+ Local<Object> events = events_v->ToObject();
+ Local<Value> listeners_v = events->Get(event);
+ return (listeners_v->IsFunction() || listeners_v->IsArray());
+}
+
} // namespace node
View
@@ -37,6 +37,7 @@ class EventEmitter : public ObjectWrap {
v8::Handle<v8::Value> argv[]);
protected:
+ bool HaveListeners(v8::Handle<v8::String> event);
EventEmitter() : ObjectWrap () { }
};

0 comments on commit 1c99451

Please sign in to comment.