We earn commission when you buy through affiliate links.

This does not influence our reviews or recommendations.Learn more.

Laravel is many things.

Article image

But fast isnt one of them.

Lets learn some tricks of the trade to make it go faster!

No PHP developer is untouched by Laravel these days.

Article image

Ive personally come across 40, and there could easily be more if youre using other libraries and plugins.

Point being, by default this layers upon layers of code, make Laravel slow.

How slow is Laravel?

Article image

Honestly, its plain impossible to answer this question for several reasons.

First, theres no accepted, objective, sensible standard for measuring the speed of Web apps.

Faster or slower compared to what?

Article image

that its plain silly to talk about speed.

A very fast Web app with a very slow database is a very slow web app.

But this uncertainty is precisely why benchmarks are popular.

Article image

Even though they mean nothing (seethis), they providesomeframe of reference and help us from going mad.

Yes, dear friends, Laravel comes last!

Normally, this slowness doesnt feature in applications because our everyday web apps rarely hit high numbers.

Article image

But hey, cheer up!

This article isnt about what cant be done but about what can be done.

The good news is, you could do a lot to make your Laravel app go faster.

Yes, no kidding.

Lets get to it.

By the way, theres no rationale behind the numbering, and its not an accepted standard.

I just made these up.

And now, finally, we arrive at the promised land.

Consider a very common scenario: displaying the list of all orders placed by a given list of customers.

In Laravel, we might imagine a controller function that does the job like this:

Sweet!

And more importantly, elegant, beautiful.

Unfortunately, its adisastrousway to write code in Laravel.

As a result, all the returned rows get stored in the collection$customersinside the controller function.

Now we loop over each customer one by one and get their orders.

This executes the following query .

as many times as there are customers.

This is where the name n+1 comes from.

Can we do better?

Imagine what would happen if there were 10,000 customers to process!

Or God forbid if we also wanted to display the items contained in every order!

Remember, the name of the technique is eager loading, and its almost always a good idea.

Cache the configuration!

One of the reasons for Laravels flexibility is the tons of configuration files that are part of the framework.

Want to change how/where the images are stored?

Well, just change theconfig/filesystems.phpfile (at least as of writing).

Want to work with multiple queue drivers?

Feel free to describe them inconfig/queue.php.

Except that its stupid if nothing has changed in the last few days!

The next time theres a Web request, Laravel will simply read this single file and get going.

That said, configuration caching is an extremely delicate operation that can blow up in your face.

It does make sense when you think about it.

In other words, youre expecting the environment to stay static, which is what.envfiles are for.

These are available in theconfig/app.phpfile as part of the’providers’array key.

Now, you may need all of them, but its unlikely.

All in all, almost half of these are unnecessary for my use case.

Take a long, hard look at your app.

Does it need all of these service providers?

But for Gods sake, just dont blindly comment out these services and push to production!

In other words, be careful of where you add/apply a new middleware.

In such cases, doing aDB::raw()and writing the query by hand is preferred.

Laravel has built-in support for several types ofcaching.

These options are also known ascache drivers.

Even informalbenchmarksshow that RAM outperforms SSD by 10-20 times when it comes to speed.

This is especially true if you cant stand large files like me and end up splitting yourweb.phpandapi.phpover several files.

Coincidentally, theyre also the largest consumers of bandwidth and one of the biggest reasons for slow apps/websites.

Instead, go for a solution likeCloudinarythat automatically resizes and optimizes images on the fly.

A really bad piece of UX, Im sure youll agree.

Mix is a lightweight (and delightful, in all honesty!)

And, for newbiew to become a Laravel master, check out thisonline course.

May your apps run much, much faster!