Skip to content

Commit

Permalink
Replace forEach with for in publish for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
goldfire committed Mar 2, 2018
1 parent 94b5c3e commit dca21ab
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/democracy.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,13 @@ class Democracy extends EventEmitter {
*/
publish(channel, msg) {
// Loop through all nodes and send the message to ones that are subscribed.
Object.keys(this._nodes).forEach((id) => {
const ids = Object.keys(this._nodes);
for (let i = 0; i < ids.length; i += 1) {
const id = ids[i];
if (this._nodes[id] && this._nodes[id].channels.includes(channel)) {
this.send(channel, msg, id);
}
});
}

return this;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "democracy",
"version": "2.0.0",
"version": "2.0.1",
"description": "Node.js unicast discovery, master-slave elections and pub/sub.",
"homepage": "https://github.com/goldfire/democracy.js",
"keywords": [
Expand Down

0 comments on commit dca21ab

Please sign in to comment.