You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
events: prefix events to prevent breaking on known object properties #728
In the EventEmitter the events are stored in an plain Object instance. The event names that you use are added directly as property on the object so when you event names such as __proto__ it will break. One solution is to prefix the keys with a char such as ~.
@meandmycode You are comparing linear array to hash map.. you need to either use string keys or very sparse numeric indices to force the object into a dictionary mode.
Secondly the comparison is not fair unless you also factor in the cost of checking if a key is "__proto__".
So when using an object as a true string hash map (arbitrary string is supported, including proto) vs using Map, the speed is not surprisingly very much equal http://jsperf.com/map-vs-object-as-hashes/24
Also, @bnoordhuis just pointed out on another issue that Object.create(null) is 15-30x slower than an object literal. I don't think it's worth it in this case.
Activity
cjihrig commentedon Feb 5, 2015
We just solved a similar problem with console timer labels. We ended up going with a
Map, butObject.create(null)worked equally as well.Fishrock123 commentedon Feb 5, 2015
Sounds like a spot to use
Map. (Assuming it has decent perf)3rd-Eden commentedon Feb 5, 2015
I don't think that the performance of Map and Weakmap are better than
Object.create(null)meandmycode commentedon Feb 5, 2015
According to this micro-benchmark (yep) you could expect map to be twice as slow:
http://jsperf.com/map-vs-object-as-hashes/14
Hash access may well not be the/a performance bottleneck in events though.
petkaantonov commentedon Feb 9, 2015
@meandmycode You are comparing linear array to hash map.. you need to either use string keys or very sparse numeric indices to force the object into a dictionary mode.
Secondly the comparison is not fair unless you also factor in the cost of checking if a key is
"__proto__".So when using an object as a true string hash map (arbitrary string is supported, including proto) vs using Map, the speed is not surprisingly very much equal http://jsperf.com/map-vs-object-as-hashes/24
cjihrig commentedon Feb 9, 2015
One thing to note is that the current version of v8 hasn't required explicit checking for
__proto__.cjihrig commentedon Feb 9, 2015
Also, @bnoordhuis just pointed out on another issue that
Object.create(null)is 15-30x slower than an object literal. I don't think it's worth it in this case.test: add test for known issue with event emitters
Trott commentedon Mar 10, 2016
I opened pull request to add a test for this to
known_issues: #56492 remaining items
test: add batch of known issue tests
test: add batch of known issue tests
test: add batch of known issue tests
test: add batch of known issue tests
events: don't inherit from Object.prototype
events: don't inherit from Object.prototype
events: don't inherit from Object.prototype