Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

API Return: Kilobytes Optimization

Premium
2:59

When building Laravel applications that serve as APIs, the amount of data transferred over the network is equally critical for performance. This lesson explores how to minimize API response size.


The Problem: Unnecessary Data Transfer

Even with server-side optimizations like query limits in place, APIs often return excessive data that clients don't actually need. This creates several issues:

  • Increased bandwidth usage
  • Slower response times for clients
  • Higher data consumption for mobile users
  • Unnecessary processing on client devices

Consider a simple API endpoint returning posts with their associated users:

// Unoptimized approach
return Post::with('user')
->latest()
->take(100)
->get();

Testing this endpoint with an API client reveals:

  • Response time: 43ms (relatively fast)
  • Data transferred: 516KB

The issue? We're returning every column from both tables, including lengthy post content and user descriptions that the client doesn't need.


The Solution: Return Only What Is Needed

We can limit post only to...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

No comments yet…