From 7d2318dce684294171d66e65997b28cdf4e46086 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Mon, 13 Feb 2017 17:27:16 -0800 Subject: [PATCH] Use unordered-array-remove for better performance! --- lib/client/http-tracker.js | 3 ++- lib/client/udp-tracker.js | 3 ++- lib/server/swarm.js | 3 ++- package.json | 1 + 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/client/http-tracker.js b/lib/client/http-tracker.js index faee1de..629ae16 100644 --- a/lib/client/http-tracker.js +++ b/lib/client/http-tracker.js @@ -1,5 +1,6 @@ module.exports = HTTPTracker +var arrayRemove = require('unordered-array-remove') var bencode = require('bencode') var compact2string = require('compact2string') var debug = require('debug')('bittorrent-tracker:http-tracker') @@ -133,7 +134,7 @@ HTTPTracker.prototype._request = function (requestUrl, params, cb) { function cleanup () { if (request) { - self.cleanupFns.splice(self.cleanupFns.indexOf(cleanup), 1) + arrayRemove(self.cleanupFns, self.cleanupFns.indexOf(cleanup)) request.abort() request = null } diff --git a/lib/client/udp-tracker.js b/lib/client/udp-tracker.js index ae6e06c..3198edc 100644 --- a/lib/client/udp-tracker.js +++ b/lib/client/udp-tracker.js @@ -1,5 +1,6 @@ module.exports = UDPTracker +var arrayRemove = require('unordered-array-remove') var BN = require('bn.js') var Buffer = require('safe-buffer').Buffer var compact2string = require('compact2string') @@ -110,7 +111,7 @@ UDPTracker.prototype._request = function (opts) { timeout = null } if (socket) { - self.cleanupFns.splice(self.cleanupFns.indexOf(cleanup), 1) + arrayRemove(self.cleanupFns, self.cleanupFns.indexOf(cleanup)) socket.removeListener('error', onError) socket.removeListener('message', onSocketMessage) socket.on('error', noop) // ignore all future errors diff --git a/lib/server/swarm.js b/lib/server/swarm.js index 5203318..9054268 100644 --- a/lib/server/swarm.js +++ b/lib/server/swarm.js @@ -1,5 +1,6 @@ module.exports = Swarm +var arrayRemove = require('unordered-array-remove') var debug = require('debug')('bittorrent-tracker:swarm') var LRU = require('lru') var randomIterate = require('random-iterate') @@ -103,7 +104,7 @@ Swarm.prototype._onAnnounceStopped = function (params, peer, id) { // swarms that this peer is participating in. if (peer.socket) { var index = peer.socket.infoHashes.indexOf(this.infoHash) - peer.socket.infoHashes.splice(index, 1) + arrayRemove(peer.socket.infoHashes, index) } this.peers.remove(id) diff --git a/package.json b/package.json index b7de0cb..81c3a6f 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "simple-websocket": "^4.0.0", "string2compact": "^1.1.1", "uniq": "^1.0.1", + "unordered-array-remove": "^1.0.2", "ws": "^1.0.0", "xtend": "^4.0.0" },