100 Days of Code Challenge!

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.

  1. What I Was Doing Wrong (Base64):
    // :x: 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
    });

  2. Why Base64 Hurt Performance:

  • :chart_with_upwards_trend: 30-40% Larger Payloads - Increased upload times
  • :snail: Slow DB Queries - Huge text fields bogging down operations
  • :arrows_counterclockwise: Double Processing - Encoding/decoding overhead
  • :floppy_disk: Storage Waste - Same image stored in DB and filesystem
  1. The Right Approach (Multer + Filenames):
    // :white_check_mark: 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
    });

  2. What Changed:

  • :rocket: Faster Uploads - Binary files instead of text blobs
  • :chart_with_downwards_trend: Reduced DB Size - 100x smaller entries (filename vs Base64)
  • :zap: Instant Rendering - Direct image URLs instead of parsing
  • :lock: Better Security - File type validation and size limits
  1. Performance Wins:
  • Upload time :stopwatch:: 8s → 2s (for 4MB images)
  • Database size :floppy_disk:: Reduced by 78%
  • Memory usage :brain:: 65% less Node.js heap usage
  • Page load :page_facing_up:: 3x faster post listings
  1. Key Implementation Tips:
  • Always use FormData for 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