Day 75 of 100daysofcode : From Base64 Bloat to Efficient File Uploads
Today I fixed my image upload performance issues by ditching Base64 for proper file handling with Multer! Here’s how I transformed slow uploads into smooth operations.
-
What I Was Doing Wrong (Base64):
//
Old Approach - Base64 String
const base64Image = req.body.image; // Huge string!
const newPost = await Post.create({
title: req.body.title,
image: base64Image // 33% size increase + DB bloat
}); -
Why Base64 Hurt Performance:
30-40% Larger Payloads - Increased upload times
Slow DB Queries - Huge text fields bogging down operations
Double Processing - Encoding/decoding overhead
Storage Waste - Same image stored in DB and filesystem
-
The Right Approach (Multer + Filenames):
//
New Solution - File Handling
const upload = multer({ storage });
router.post(‘/upload’, upload.single(‘image’), (req, file) => {
// Store just filename - small DB entry
image: req.file.filename
}); -
What Changed:
Faster Uploads - Binary files instead of text blobs
Reduced DB Size - 100x smaller entries (filename vs Base64)
Instant Rendering - Direct image URLs instead of parsing
Better Security - File type validation and size limits
- Performance Wins:
- Upload time
: 8s → 2s (for 4MB images) - Database size
: Reduced by 78% - Memory usage
: 65% less Node.js heap usage - Page load
: 3x faster post listings
- Key Implementation Tips:
- Always use
FormDatafor file uploads - Store only filenames/URLs in database
- Serve images as static assets
- Set sane file size limits (5MB shown)
- Use CDN for image distribution
So glad I switched from Base64 to proper file handling - performance gains were massive!
lebanon-mug