DEV Community

Discussion on: Make Your Laravel Website Super Fast,Optimize Laravel Performance

Collapse
 
rvxlab profile image
RVxLab

You're making some very good points, but I disagree with the wording of this statement:

Larvel comes with Eager loading (ORM) so use it , avoid writing your own raw query.

Avoid writing your own raw query unless you know what you're doing. Sometimes it's needed to write raw queries but when you do so ensure you don't create an SQL Injection vulnerability.

A use case for this is using the Haversine formula to calculate distance between coordinates.

Sometimes doing it yourself is just what you need.

Also on the topic of PurgeCSS you fail to mention that if you're using dynamically built class names PurgeCSS will not work as you'd expect.

As PurgeCSS checks things statically, classes built like so will not be safe unless you put it in the safelist option:

// Assuming status is one of the following: success, error, info

<div class="{{ 'alert-'.$status }}">
    {{ $someAlertMessage }}
</div>
Enter fullscreen mode Exit fullscreen mode

PurgeCSS will not understand that alert-success, alert-error and alert-info are in use, thus they will be purged in a production build.

Instead, you'd use something like this:


function getAlertClass(string $status): string
{
    switch ($status) {
        case 'success':
            return 'alert-success';
        case 'error': 
            return 'alert-error';
        case 'info':
        default:
            return 'alert-info';
    }
}

<div class="{{ getAlertClass($status) }}">
    {{ $someAlertMessage }}
</div>
Enter fullscreen mode Exit fullscreen mode
Collapse
 
juststevemcd profile image
Steve McDougall

I wrote a scope for the haversine formula which enablede to keep using the ORM πŸ˜…

If you're really looking at improving performance of a Laravel application you have two options in 2021:

  • Laravel Vapor
  • Laravel Octaine

Serverless, or a long running process. There are micro optimization a you can do with a Laravel app, but none that make a huge difference straight away.

Collapse
 
ajayyadav profile image
Ajay Yadav

@rvxlab you are right brother 😊✌️ I couldn't explain it briefly , but u did β˜ΊοΈπŸ‘ŒβœŒοΈ