Skip to content

Performance Optimization

Optimize Pocat for better performance, faster downloads, and improved scalability.

yt-dlp (Recommended):

  • 95%+ success rate
  • Fastest downloads
  • Best format selection
{
"youtubeUrl": "URL",
"downloader": "yt-dlp"
}

Balance quality vs speed:

{
"quality": "720p" // Good balance
}

Quality performance comparison:

  • 144p: ~10MB, fastest download
  • 360p: ~25MB, good for mobile
  • 720p: ~130MB, recommended default
  • 1080p: ~330MB, high quality
  • 4K: ~2GB, slowest but best quality

Limit concurrent downloads:

const maxConcurrent = 3;
const downloadQueue = [];
async function processQueue() {
const batch = downloadQueue.splice(0, maxConcurrent);
await Promise.all(batch.map(download));
}

Node.js memory settings:

Terminal window
# Increase heap size
node --max-old-space-size=4096 server.js
# Enable garbage collection optimization
node --optimize-for-size server.js

Monitor memory usage:

setInterval(() => {
const used = process.memoryUsage();
console.log('Memory usage:', {
rss: Math.round(used.rss / 1024 / 1024) + 'MB',
heapUsed: Math.round(used.heapUsed / 1024 / 1024) + 'MB'
});
}, 30000);

Use clustering:

const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
} else {
// Start your app
require('./server.js');
}

SQLite optimizations:

-- Enable WAL mode
PRAGMA journal_mode=WAL;
-- Increase cache size
PRAGMA cache_size=10000;
-- Optimize for speed
PRAGMA synchronous=NORMAL;

Add database indexes:

CREATE INDEX idx_projects_status ON projects(status);
CREATE INDEX idx_projects_created_at ON projects(created_at);
const compression = require('compression');
app.use(compression());

Response caching:

const cache = new Map();
app.get('/api/projects/:id', (req, res) => {
const cacheKey = `project_${req.params.id}`;
if (cache.has(cacheKey)) {
return res.json(cache.get(cacheKey));
}
// Fetch data and cache it
const data = fetchProject(req.params.id);
cache.set(cacheKey, data);
res.json(data);
});

Implement smart rate limiting:

const rateLimit = require('express-rate-limit');
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
message: 'Too many requests, please try again later'
});
app.use('/api/', limiter);

Use SSD storage for better I/O:

  • Store downloads on SSD
  • Use separate disk for database
  • Implement file cleanup policies

File cleanup automation:

const cron = require('node-cron');
// Clean up files older than 7 days
cron.schedule('0 2 * * *', () => {
const sevenDaysAgo = Date.now() - (7 * 24 * 60 * 60 * 1000);
fs.readdir('downloads/', (err, files) => {
files.forEach(file => {
fs.stat(`downloads/${file}`, (err, stats) => {
if (stats.mtime.getTime() < sevenDaysAgo) {
fs.unlink(`downloads/${file}`, () => {});
}
});
});
});
});

Stream large files:

app.get('/download/:id', (req, res) => {
const filePath = `downloads/project_${req.params.id}.mp4`;
const stat = fs.statSync(filePath);
res.writeHead(200, {
'Content-Type': 'video/mp4',
'Content-Length': stat.size,
'Accept-Ranges': 'bytes'
});
const stream = fs.createReadStream(filePath);
stream.pipe(res);
});
const performanceMonitor = {
downloadTimes: [],
apiResponseTimes: [],
recordDownload(duration) {
this.downloadTimes.push(duration);
if (this.downloadTimes.length > 100) {
this.downloadTimes.shift();
}
},
getAverageDownloadTime() {
return this.downloadTimes.reduce((a, b) => a + b, 0) / this.downloadTimes.length;
}
};
app.get('/health', (req, res) => {
const health = {
status: 'ok',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
memory: process.memoryUsage(),
downloads: {
active: getActiveDownloads(),
completed: getCompletedDownloads(),
failed: getFailedDownloads()
}
};
res.json(health);
});
NODE_ENV=production
NODE_OPTIONS="--max-old-space-size=4096"
UV_THREADPOOL_SIZE=16

PM2 configuration:

ecosystem.config.js
module.exports = {
apps: [{
name: 'pocat',
script: './server.js',
instances: 'max',
exec_mode: 'cluster',
env: {
NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production',
PORT: 3333
}
}]
};

Nginx configuration:

upstream pocat_backend {
server 127.0.0.1:3333;
server 127.0.0.1:3334;
server 127.0.0.1:3335;
}
server {
location / {
proxy_pass http://pocat_backend;
}
}
Terminal window
# Install Apache Bench
sudo apt install apache2-utils
# Test API performance
ab -n 1000 -c 10 http://localhost:3333/api/health
# Test download endpoint
ab -n 100 -c 5 -p post_data.json -T application/json http://localhost:3333/v2/projects
  • PM2 Monitor: pm2 monit
  • htop: System resource monitoring
  • iotop: Disk I/O monitoring
  • New Relic: Application performance monitoring