0
0
Laravelframework~8 mins

Creating jobs in Laravel - Performance Optimization Steps

Choose your learning style9 modes available
Performance: Creating jobs
MEDIUM IMPACT
This affects how background tasks are handled, impacting page responsiveness and server load during user interactions.
Handling time-consuming tasks like sending emails or processing images
Laravel
<?php
// Controller method
public function sendEmail(Request $request) {
    SendEmailJob::dispatch($request->user());
    return response()->json(['status' => 'Email queued']);
}

// Job class
use IlluminateContractsQueueShouldQueue;

class SendEmailJob implements ShouldQueue {
    protected User $user;

    public function __construct(User $user) {
        $this->user = $user;
    }

    public function handle() {
        Mail::to($this->user)->send(new WelcomeEmail());
    }
}
Queues the email sending to a background job, freeing the request to respond immediately.
📈 Performance GainNon-blocking request, improves INP by avoiding delays during user interaction.
Handling time-consuming tasks like sending emails or processing images
Laravel
<?php
// Controller method
public function sendEmail(Request $request) {
    Mail::to($request->user())->send(new WelcomeEmail());
    return response()->json(['status' => 'Email sent']);
}
Sending email directly blocks the request, causing slow response and poor user experience.
📉 Performance CostBlocks rendering and response for the entire email sending duration, increasing INP.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Synchronous heavy task in controllerMinimal00[X] Bad
Queued job for heavy taskMinimal00[OK] Good
Rendering Pipeline
Creating jobs moves heavy processing out of the request lifecycle, reducing main thread blocking and improving interaction responsiveness.
JavaScript Event Loop
Server Request Handling
Network Response
⚠️ BottleneckServer request handling blocks when heavy tasks run synchronously.
Core Web Vital Affected
INP
This affects how background tasks are handled, impacting page responsiveness and server load during user interactions.
Optimization Tips
1Always queue heavy or slow tasks to keep requests fast.
2Use Laravel jobs to improve interaction responsiveness (INP).
3Avoid blocking the main request with long-running operations.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using jobs in Laravel for heavy tasks?
AThey improve CSS rendering speed.
BThey reduce the size of the HTML sent to the browser.
CThey allow the main request to respond faster by deferring work.
DThey increase the number of DOM nodes.
DevTools: Network
How to check: Open DevTools, go to Network tab, perform the action triggering the task, and observe response time.
What to look for: Long response times indicate blocking synchronous tasks; short response with background processing indicates good performance.