Performance: Active Job framework
Active Job affects background job processing, which impacts user interaction responsiveness and server load handling.
class UsersController < ApplicationController def create WelcomeEmailJob.perform_later(@user.id) # other code end end class WelcomeEmailJob < ApplicationJob queue_as :default def perform(user_id) user = User.find(user_id) UserMailer.welcome_email(user).deliver_now end end
class UsersController < ApplicationController def create UserMailer.welcome_email(@user).deliver_now # other code end end
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous task in request | N/A | N/A | Blocks response, delays paint | [X] Bad |
| Background job with Active Job | N/A | N/A | Non-blocking, faster response | [OK] Good |
perform_later to enqueue jobs to run asynchronously.perform_now runs immediately, enqueue and run_async are not valid Active Job methods.class GreetingJob < ApplicationJob
def perform(name)
puts "Hello, #{name}!"
end
end
GreetingJob.perform_later('Alice')perform_later method enqueues the job to run asynchronously in the background.puts inside perform runs when the job executes later, not immediately.class EmailJob < ApplicationJob
def perform
UserMailer.welcome_email.deliver_now
end
endperform_now ignores verification and blocks the request.Pass the test and this pattern moves unknown