Optimize WebTorrent for maximum speed, efficiency, and resource usage
WebTorrent is designed to be fast and efficient, but there are many ways to optimize performance for your specific use case. This guide covers bandwidth throttling, connection management, memory optimization, and platform-specific tuning.
Control bandwidth usage across all torrents with global throttle settings:
import WebTorrent from 'webtorrent'const client = new WebTorrent({ downloadLimit: 1000000, // 1 MB/s in bytes/sec uploadLimit: 500000 // 500 KB/s in bytes/sec})// Or set dynamicallyclient.throttleDownload(2000000) // 2 MB/sclient.throttleUpload(1000000) // 1 MB/s// Disable throttling (use full bandwidth)client.throttleDownload(-1)client.throttleUpload(-1)// Block all downloads/uploadsclient.throttleDownload(0)client.throttleUpload(0)
Throttling affects all torrents in the client. Use it to prevent WebTorrent from consuming all available bandwidth.
function getSeedRatio(torrent) { return torrent.uploaded / (torrent.downloaded || 1)}// Global ratio across all torrentsconsole.log('Client ratio:', client.ratio)// Per-torrent ratioconsole.log('Torrent ratio:', getSeedRatio(torrent))
Control the number of simultaneous peer connections:
const client = new WebTorrent({ maxConns: 55 // default, max connections per torrent})
More connections don’t always mean better performance. Too many connections can overwhelm your network and CPU. The default of 55 is optimal for most use cases.
Choose between sequential and rarest-first piece selection:
// Sequential: download pieces in order (best for streaming)client.add(torrentId, { strategy: 'sequential'})// Rarest: download rarest pieces first (best for seeding)client.add(torrentId, { strategy: 'rarest'})
Sequential Strategy
Best for: Video/audio streaming, progressive downloadsDownloads pieces in order from start to finish. Enables immediate playback of media files.
Rarest-First Strategy
Best for: Maximizing swarm health, fast completionPrioritizes rarest pieces to improve overall availability in the swarm.
Download only specific files to save bandwidth and storage:
client.add(torrentId, torrent => { // Deselect all files initially torrent.deselect(0, torrent.pieces.length - 1, false) // Select only video files torrent.files.forEach(file => { if (file.name.endsWith('.mp4') || file.name.endsWith('.mkv')) { file.select() } })})// Or start with no pieces selectedclient.add(torrentId, { deselect: true}, torrent => { // Manually select what you need torrent.files[0].select()})
Dynamic File Selection
// Select files on demandfunction selectFileByIndex(torrent, index) { const file = torrent.files[index] if (!file.progress === 1) { file.select() console.log('Selected:', file.name) }}// Deselect completed or unwanted filesfunction deselectFile(torrent, index) { const file = torrent.files[index] file.deselect() console.log('Deselected:', file.name)}// Priority selection with auto-deselecttorrent.files.forEach((file, i) => { file.on('done', () => { console.log('Completed:', file.name) // Optionally deselect to free up resources file.deselect() })})
Use uTP for better congestion control and NAT traversal:
import WebTorrent from 'webtorrent'// Check if uTP is supportedif (WebTorrent.UTP_SUPPORT) { const client = new WebTorrent({ utp: true // enabled by default if utp-native is installed })}
Install the optional dependency: npm install utp-nativeuTP provides better bandwidth sharing with other applications and improved performance in congested networks.
// In Node.jsprocess.env.DEBUG = 'webtorrent*'// Or specific modulesprocess.env.DEBUG = 'webtorrent,bittorrent-dht'// In browser consolelocalStorage.setItem('debug', 'webtorrent*')// Then reload the page