How to optimize function memory and duration on Vercel
Optimize Vercel functions by configuring memory allocation in vercel.json, implementing efficient code patterns, and monitoring execution metrics. Use proper caching strategies and minimize cold starts to reduce duration and memory usage.
Prerequisites
- Active Vercel account
- Deployed project with serverless functions
- Access to project settings
- Basic understanding of serverless architecture
Step-by-Step Instructions
Configure function memory limits in vercel.json
vercel.json file in the project root. Add memory configuration for specific functions:{
"functions": {
"api/heavy-function.js": {
"memory": 1024
},
"api/light-function.js": {
"memory": 128
}
}
}Set memory values between 128MB and 3008MB based on your function's requirements.
Set maximum execution duration
maxDuration property in your vercel.json to prevent functions from running indefinitely:{
"functions": {
"api/*.js": {
"maxDuration": 10
}
}
}Duration is set in seconds. Free plans are limited to 10 seconds, Pro plans up to 60 seconds, and Enterprise up to 900 seconds.
Optimize code for memory efficiency
- Use
constandletinstead ofvarfor proper garbage collection - Avoid loading large dependencies unnecessarily
- Process data in chunks rather than loading everything into memory
- Use streaming for large file operations
- Clear variables when done:
largeObject = null
Implement connection pooling and caching
// Global connection variable
let cachedConnection = null;
export default async function handler(req, res) {
if (!cachedConnection) {
cachedConnection = await createConnection();
}
// Use cached connection
}Use external caching services like Redis or Vercel's Edge Config for shared state.
Monitor function performance
- Memory usage patterns
- Execution duration
- Cold start frequency
- Error rates and timeouts
Click on individual function invocations to see detailed metrics and logs. Use this data to identify optimization opportunities.
Minimize cold start impact
- Keeping function code lightweight
- Using Vercel's Edge Functions for simple operations
- Implementing connection warming strategies
- Avoiding heavy initialization code
- Using
importstatements at the top level only for critical dependencies
Deploy and test optimizations
vercel --prod or through Git integration. Test performance improvements by:- Running load tests on optimized endpoints
- Comparing before/after metrics in the dashboard
- Monitoring memory usage under different loads
- Verifying execution times meet requirements
Common Issues & Troubleshooting
Function exceeds memory limit and crashes
Increase memory allocation in vercel.json or optimize code to use less memory. Check for memory leaks and ensure large objects are properly disposed of.
Function times out before completion
Increase maxDuration in vercel.json or optimize code performance. Consider breaking large operations into smaller chunks or using background jobs for heavy processing.
High memory usage despite optimization
Profile your function using console.log(process.memoryUsage()) to identify memory hotspots. Check for circular references and ensure proper cleanup of event listeners and timers.
Configuration changes not taking effect
Ensure vercel.json is in the project root and properly formatted. Redeploy the project completely using vercel --prod --force to apply configuration changes.