Skip to content

events: prefix events to prevent breaking on known object properties #728

Description

@3rd-Eden

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 ~.

Example case:

var EventEmitter = require('events').EventEmitter;
var e = new EventEmitter();

e.on('__proto__', function (bar) {
  console.log('foo', bar);
});
e.emit('__proto__', 1);

Willing to create pull request if bug requires fixing ;)

Activity

  1. added
    eventsIssues and PRs related to EventEmitter and the events module.
    on Feb 5, 2015
  2. cjihrig commented on Feb 5, 2015

    @cjihrig
    Contributor

    We just solved a similar problem with console timer labels. We ended up going with a Map, but Object.create(null) worked equally as well.

  3. Fishrock123 commented on Feb 5, 2015

    @Fishrock123
    Contributor

    Sounds like a spot to use Map. (Assuming it has decent perf)

  4. 3rd-Eden commented on Feb 5, 2015

    @3rd-Eden
    ContributorAuthor

    I don't think that the performance of Map and Weakmap are better than Object.create(null)

  5. meandmycode commented on Feb 5, 2015

    @meandmycode

    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.

  6. petkaantonov commented on Feb 9, 2015

    @petkaantonov
    Contributor

    @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

  7. cjihrig commented on Feb 9, 2015

    @cjihrig
    Contributor

    One thing to note is that the current version of v8 hasn't required explicit checking for __proto__.

  8. cjihrig commented on Feb 9, 2015

    @cjihrig
    Contributor

    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.

  9. Trott commented on Mar 10, 2016

    @Trott
    Member

    I opened pull request to add a test for this to known_issues: #5649

  10. 2 remaining items

  11. added a commit that references this issue on Mar 14, 2016
  12. added a commit that references this issue on Mar 16, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues and PRs for confirmed bugs.eventsIssues and PRs related to EventEmitter and the events module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions