Reputation: 2226
I wonder whether I should create one EventEmitter and pass an object to it in which there will be a key to run some code inside the callback function depending on the key inside the object (I would have 15 different situations) or should I create 15 eventEmitters depending on 15 different names of message? I wonder if creating multiple evenEmitter will slow down, take RAM or CPU resources of my NodeJS instance
something like this:
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
myEmitter.on('event1', (data) => console.log(data)); // receiver code
myEmitter.on('event2', (data) => console.log(data)); //receiver code
myEmitter.emit('event1','event1'); //sender code
myEmitter.emit('event2','event2'); //sender code
//event3
//event4
//...
or something like that:
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
let obj1 = { msgType:'event1',data:'one exemple'}; // sender code
let obj2 = { msgType:'event2',data:'another exemple'}; //sender code
myEmitter.on('event', (data) => { //receiver code
if (data.msgType=="event1"){
console.log("event1");
}
if (data.msgType=="event2"){
console.log("event2");
}
});
myEmitter.emit('event',obj1); //sender code
myEmitter.emit('event',obj2); //sender
Upvotes: 1
Views: 1456
Reputation: 707876
All an event emitter is just a Javascript object that keeps a data structure containing listeners for various methods. An inactive listener in an emitter consumes no CPU and takes only as much RAM as storing a callback reference and a message name that it's listening for (so, almost no RAM).
You can have as many emitters as make sense for your code. They are cheap and efficient. You can literally just think of them as a static array of listeners. When you do .addListener()
, it adds an item to the array. When you do .removeListener()
, it removes an item from the array. When you do .emit()
, it finds the listeners that match that particular message and calls them (just function calls).
or should I create 15 eventEmitters depending on 15 different names of message?
eventEmitters were built to handle many different message names. So, just because you have 15 different messages, that is no reason to make 15 unique eventEmitters. You can easily just use one eventEmitter and call .emit()
on it with all your different messages.
The reason to make multiple eventEmitters has to do with the design and architecture of your code. If you have a component that you want to be modular and reusable and it uses an eventEmitter
, then it may want to create its own emitter and make that available to its clients just so it doesn't have to have a dependency with other code that also wants to use an eventEmitter, but does not otherwise have anything to do with this particular module. So, it's an architectural and code organization question, not one of runtime efficiency. Create only as many eventEmitters as your architecture naturally desires and no more.
I wonder if creating multiple eventEmitter will slow down, take RAM or CPU resources of my NodeJS instance
No, it will not. Each eventEmitter takes a very small amount of memory to just initialize its basic instance data, but this is so small that you could probably not even measure the difference between 1 or these and 15 of them.
I wonder if I should create one EventEmitter and pass an object to it in which there will be a key to run some code inside the callback function depending on the key inside the object.
You are free to design your code that way if you want, but you're making extra work for yourself and writing code that probably isn't as clean as it could be. A big advantage of eventEmitters is that they maintain a specific set of listeners for each separate messages. If you use one generic message and then embed the actual message inside an object that you pass to the .emit()
call, then you're just throwing away features the eventEmitter has and putting a burden on the calling code to figure out which sub-message is actually contained in this event.
In general, this would be an inefficient way to use an EventEmitter
. Instead, put the actual event name in the .emit()
and let code register a listener for the actual event names it wants to listen to.
So, of the two schemes you show, I much, much prefer the first one. That's how EventEmitters
were designed to be used and how they were designed to help you. There could be situations where you have to have a generic message with your own sub-routing, but unless you are sure you require that, you should not be adding the extra level of complexity and you're throwing away functionality that the EventEmitter will do for you for free.
Also, you show this code:
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
Do you realize that there's no need to subclass an EventEmitter just to use one. You would only subclass it if you're going to add or override methods on your subclass. But this code shows no actual new methods or overrides so there's no point to doing it that way.
If you just want to use an EventEmitter, you just create one:
const myEmitter = new EventEmitter();
Upvotes: 4