Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf: Use delete keyword to avoid tons of leftover keys in plain objects #708

Merged
merged 1 commit into from Mar 29, 2016
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Perf: Use delete keyword to avoid tons of leftover keys in plain objects

  • Loading branch information
feross committed Mar 29, 2016
commit ca4576f377ce26b6c41b41d5caa9c6bc426e0142
@@ -90,8 +90,7 @@ Object.defineProperty(Swarm.prototype, 'numConns', {
var self = this
var numConns = 0
for (var id in self._peers) {
var peer = self._peers[id]
if (peer && peer.connected) numConns += 1
if (self._peers[id].connected) numConns += 1
}
return numConns
}
@@ -218,7 +217,7 @@ Swarm.prototype.removePeer = function (id) {

debug('removePeer %s', id)

self._peers[id] = null
delete self._peers[id]
self._peersLength -= 1

peer.destroy()
@@ -64,12 +64,7 @@ TCPPool.removeSwarm = function (swarm, cb) {
if (!pool) return cb()
pool.removeSwarm(swarm)

var numSwarms = 0
for (var infoHash in pool.swarms) {
var s = pool.swarms[infoHash]
if (s) numSwarms += 1
}
if (numSwarms === 0) pool.destroy(cb)
if (Object.keys(pool.swarms).length === 0) pool.destroy(cb)
else process.nextTick(cb)
}

@@ -85,7 +80,7 @@ TCPPool.removeSwarm = function (swarm, cb) {
TCPPool.getDefaultListenPort = function (infoHash) {
for (var port in tcpPools) {
var pool = tcpPools[port]
if (pool && !pool.swarms[infoHash]) return pool.port
if (!pool.swarms[infoHash]) return pool.port
}
return 0
}
@@ -125,7 +120,7 @@ TCPPool.prototype.addSwarm = function (swarm) {
TCPPool.prototype.removeSwarm = function (swarm) {
var self = this
debug('remove swarm %s from tcp pool %s', swarm.infoHash, self.port)
self.swarms[swarm.infoHash] = null
delete self.swarms[swarm.infoHash]
}

/**
@@ -144,7 +139,7 @@ TCPPool.prototype.destroy = function (cb) {
conn.destroy()
})

tcpPools[self.port] = null
delete tcpPools[self.port]

try {
self.server.close(cb)
@@ -165,16 +160,15 @@ TCPPool.prototype._onListening = function () {

if (port !== self.port) {
// `port` was 0 when `listen` was called; update to the port that node selected
tcpPools[self.port] = null
delete tcpPools[self.port]
self.port = port
tcpPools[self.port] = self
}

self.listening = true

for (var infoHash in self.swarms) {
var swarm = self.swarms[infoHash]
if (swarm) swarm._onListening(self.port)
self.swarms[infoHash]._onListening(self.port)
}
}

@@ -226,10 +220,8 @@ TCPPool.prototype._onError = function (err) {
self.destroy()
for (var infoHash in self.swarms) {
var swarm = self.swarms[infoHash]
if (swarm) {
self.removeSwarm(swarm)
swarm._onError(err)
}
self.removeSwarm(swarm)
swarm._onError(err)
}
}

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.