A constrained but fast EventEmitter alternative.
Don't get confused with W3 EventSource!
This is a EventEmitter alternative (for simple situations). Not Server-Sent-Event!
- No event name
- Fast for one-or-two listeners
You are welcomed to help optimize this library
npm install node-event-source
let EventSource = require('node-event-source')
let es = new EventSource()
let callback = (arg)=>console.log(arg)
es.attach( callback )
es.emit('hello world') // es.emit === callback
You can chain attach
, detach
together. But not emit
Add callback to listeners
Remove callback from listeners
Emit event, calls all listeners
If there is only one listener, this method is the listener itself
If there is only one listener, this.emit
directly set as listener
If there is more than one listener, they are stored in an Array, then called in a for-loop in this.emit()
node benchmark-EventSource.js
My result:
EE @1: 300ms
ES @1: 106ms
EE @3: 712ms
ES @3: 401ms
EE @10: 1390ms
ES @10: 1034ms
MIT