Serverless is awesome — but performance matters. Cold starts, heavy bundles, or noisy logs can hurt your Lambda’s speed and cost. Today, we’ll explore practical ways to improve Node.js performance in AWS Lambda using AWS SAM.
Whether you're building APIs or event-driven apps, these tips will help you write faster, leaner, and more cost-effective serverless functions.
🚀 1. Initialize Services Outside the Handler
This is the golden rule. Anything that can be reused between invocations should be initialized outside the handler
.
// Good ✅
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
const dbClient = new DynamoDBClient({});
export const handler = async (event) => {
// Use dbClient
};
Why? Because AWS reuses Lambda containers during warm starts, and re-initializing things like database clients inside the handler adds unnecessary overhead.
🧠 2. Use the Latest Node.js Runtime
Always use the latest supported Node.js version in AWS Lambda (at the time of writing: nodejs22.x
).
Benefits:
- Faster startup time
- Better performance and memory handling
- Access to newer language features
Runtime: nodejs22.x
💪 3. Choose ARM Architecture (Graviton)
Graviton-based Lambdas (arm64
) offer up to 34% better performance at 20% lower cost compared to x86_64
.
In your template.yml
Properties:
Architectures:
- arm64
Unless you have specific native dependencies that don't support ARM, this is a smart switch.
📦 4. Keep Lambda Package Size Small
Smaller packages = faster cold starts. Here's how to shrink things down:
- Use
esbuild
with minification and tree-shaking - Split shared code into
Layers
- Only include what you need — use
External
in yourtemplate.yml
to exclude AWS SDK packages (they're included in the Lambda runtime)
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: "es2020"
External:
- "@aws-sdk/*"
🚫 5. Avoid Wildcard Imports
Avoid doing this:
// Bad ❌
import * as AWS from "aws-sdk";
Use specific imports instead:
// Good ✅
import { S3Client } from "@aws-sdk/client-s3";
📊 6. Adjust Your Lambda Memory
Memory = power in AWS Lambda. Increasing memory also gives you more CPU.
Run performance tests at different memory levels to find the sweet spot. Sometimes doubling memory cuts execution time in half — and reduces cost.
In your template.yml
:
MemorySize: 1024
🧹 7. Don't Log Everything
Logging too much (especially in loops or high-frequency Lambdas) adds latency and cost.
Guidelines:
- Use
debug
level only when actively troubleshooting - Keep
info
logs minimal - Reserve
error
logs for actual failures
If you're using structured loggers like Powertools Logger, you can control verbosity via environment variables.
const logger = new Logger({
logLevel: process.env.LOG_LEVEL || "INFO",
});
Conclusion
Performance in Lambda isn’t just about speed — it’s about cost, cold start time, and developer experience.
✅ Recap:
- Move services outside your handler
- Use the latest Node.js runtime
- Choose ARM64 for better performance
- Keep packages small and clean
- Avoid wildcard imports
- Tune memory settings
- Log wisely
With these techniques, your Node.js Lambdas will be lean, fast, and production-ready.