diff --git a/lib/peer-discovery.js b/lib/peer-discovery.js index b1d701a..0c2b218 100644 --- a/lib/peer-discovery.js +++ b/lib/peer-discovery.js @@ -202,11 +202,9 @@ module.exports = class PeerDiscovery { if (!nodes) nodes = this._closestNodes else if (this._closestNodes !== nodes) { + const len = nodes.length for (const newer of this._closestNodes) { - for (const older of nodes) { - if (!older.id || !newer.id || b4a.equals(older.id, newer.id)) continue - nodes.push(newer) - } + if (newer.id && !hasNode(nodes, len, newer)) nodes.push(newer) } } @@ -288,4 +286,13 @@ class PeerDiscoverySession { } } +function hasNode (nodes, len, node) { + for (let i = 0; i < len; i++) { + const existing = nodes[i] + if (existing.id && b4a.equals(existing.id, node.id)) return true + } + + return false +} + function noop () {}