-
Notifications
You must be signed in to change notification settings - Fork 7.3k
events: Support prototype property name as an event type #4366
Conversation
The prototype name (such as '__proto__') as an event type does not work as my expectation. ``` > var events = require('events') > var e = new events.EventEmitter(); > e.on('__proto__', function() {}); > e.emit('__proto__'); TypeError: Object #<Object> has no method 'apply' at EventEmitter.emit (events.js:126:20) at repl:1:3 at REPLServer.self.eval (repl.js:109:21) at rli.on.self.bufferedCmd (repl.js:258:20) at REPLServer.self.eval (repl.js:116:5) at Interface.<anonymous> (repl.js:248:12) at Interface.EventEmitter.emit (events.js:96:17) at Interface._onLine (readline.js:200:10) at Interface._line (readline.js:518:8) at Interface._ttyWrite (readline.js:736:14) ``` To support these, use `Object.create(null)` instead of `{}`.
While the PR in itself LGTM, I'm not sure if it's an acceptable change. The event emitter code is considered a red hot code path and |
I see. It is true that I haven't look around about the performance. obj = {};
obj.__proto__ = null; This is not a cool code, but it is able you to use prototype property name as an event type without compromising the performance. |
Same issue as |
Hmm, really? ref: https://gist.github.com/4214426 Please tell me another case that should be checked if it exist. |
There's a bug:
|
Thanks @AlexeyKupershtokh. This code is really slower. |
lol :)
|
It's nice in theory, but in practice, we cannot do this for performance reasons. I'd be open to a doc patch explaining that |
Another option worth trying would be to prefix all event names in the object with some character, like |
+1. This is the "standard" dictionary-in-JS practice as far as I know, and would solve all of these problems. |
Whipped this up to test it out: https://github.com/isaacs/node/compare/ev_proto Seems like http_simple doesn't get any slower with this. There's a bit of added string juggling, but not too bad. |
@isaacs might be nice to use "ev:" instead of just "ev", as to not end up with things like "evview", and instead "ev:view" (event name is "view") |
Landed on b48e303.
Meh. It's internal anyway. |
Turns out that this causes a significant regression with the emit() function. Users will just have to not use events named |
The prototype property name (such as 'proto') as an event type does not work as my expectation.
To support these, use
Object.create(null)
instead of{}
.