Performance: Route constraints
MEDIUM IMPACT
Route constraints affect the server-side routing speed, impacting how quickly the server matches URLs to controllers before rendering the page.
Rails.application.routes.draw do get '/products/:id', to: 'products#show', constraints: { id: /\A\d{1,10}\z/ } end
Rails.application.routes.draw do get '/products/:id', to: 'products#show', constraints: lambda { |req| req.params[:id] =~ /\A\d{1,10}\z/ && req.params[:id].to_i < 10000000000 } end
| Pattern | Route Matching Time | Server CPU Load | Impact on LCP | Verdict |
|---|---|---|---|---|
| Complex lambda constraints with regex and logic | High (5-10ms per request) | Higher CPU usage | Delays LCP | [X] Bad |
| Simple regex constraints | Low (1-2ms per request) | Lower CPU usage | Faster LCP | [OK] Good |