0
0
Ruby on Railsframework~8 mins

Why Rails API mode exists - Performance Evidence

Choose your learning style9 modes available
Performance: Why Rails API mode exists
MEDIUM IMPACT
This affects the initial page load speed and server response time by reducing unnecessary HTML rendering and asset loading.
Building a backend that serves data only for a frontend app
Ruby on Rails
class UsersController < ActionController::API
  def index
    users = User.all
    render json: users
  end
end
API mode skips view rendering and layout, sending only JSON data which is smaller and faster to generate.
📈 Performance Gainresponse size reduced by 80%, server response time improved by 50ms
Building a backend that serves data only for a frontend app
Ruby on Rails
class UsersController < ApplicationController
  def index
    @users = User.all
    render :index # renders full HTML view with layout
  end
end
Rendering full HTML views and layouts adds extra processing and larger response size when only data is needed.
📉 Performance Costblocks rendering for extra 50-100ms per request, adds 20-50kb to response size
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Full Rails with HTML viewsN/A (server-side)N/ALarger payload delays browser paint[X] Bad
Rails API mode with JSON onlyN/A (server-side)N/ASmaller payload enables faster paint[OK] Good
Rendering Pipeline
In API mode, Rails skips template rendering and layout composition, directly serializing data to JSON. This reduces server CPU work and network payload size.
Controller Processing
View Rendering
Network Transfer
⚠️ BottleneckView Rendering stage is eliminated, reducing server CPU time.
Core Web Vital Affected
LCP
This affects the initial page load speed and server response time by reducing unnecessary HTML rendering and asset loading.
Optimization Tips
1Use Rails API mode to avoid rendering HTML views when only JSON data is needed.
2Skipping layouts and templates reduces server CPU and response size, improving load speed.
3Smaller JSON responses from API mode improve Largest Contentful Paint (LCP) on the client.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using Rails API mode?
AIt compresses HTML views to reduce size.
BIt automatically caches all responses to speed up loading.
CIt sends only JSON data without rendering HTML views or layouts.
DIt pre-renders pages on the client side.
DevTools: Network
How to check: Open DevTools, go to Network tab, reload the page or API request, and inspect the response size and content type.
What to look for: Look for smaller response sizes with 'application/json' content type indicating API mode; large HTML responses indicate full view rendering.