Laravel query performance – toBase

I’ve scoured many of the top results for “Laravel performance” in google, bing, duck duck go, etc… and am flummoxed that one of the key things to help boost Laravel performance is almost never mentioned. Specifically, I’m talking about the toBase() method in the query builder.

The toBase() method will instruct the query builder to skip the step of turning the query results in to full blown Eloquent objects, and instead, return standard PHP classes – the ‘stdClass’.

The resulting objects are bare. You can’t call any methods on them, because there are none to call.

Client::query()->where('owner_id', 7)->get();

let’s say this returns 70 client records. Each of those needs to be instantiated in to an Eloquent object. If you *need* that functionality – for example, you’re going to loop through this client list and call some operation on each client (sending a notification, or attaching resources, or whatnot..) the full Eloquent objects may be needed.

More often than not, however, I’ll see

$clients = Client::query()->where('owner_id',7)->get();
return response()->json(['clients'=>$clients);

You’ve just iterated through 70 records, created new Eloquent objects, then immediately set about converting them to JSON.

toBase() saves a tremendous amount of CPU time in the case above

$clients = Client::toBase()->query()->where('owner_id',7)->get();
return response()->json(['clients'=>$clients);

I’m not going to give full detailed benchmarks here – you can simply try this out for yourself.

I can say that with a minimally complex ‘Client’ object and about 600 records in the database:

$c = Client::query()->toBase()->get();

is roughly twice as fast as

$c = Client::query()->get(); 

A 50% speed up is nothing to sneeze at.

I recently took an API endpoint having to do with retrieving client records and associated addresses. With a little bit of rewriting – including toBase() – this went from 1.1 seconds down to ~200ms. This gets called multiple times per minute at heavy times, and having faster response times for the end users has been duly appreciated.

I believe this can be achieved in Doctrine with “HYDRATE_ARRAY” mode, and likely similar in other tech stacks (hibernate, etc).

Similar Posts

  • PHP Quality Tools

    Curious about checking out the quality of your PHP project, but don’t know where to start? https://github.com/jakzal/phpqa is a project providing docker images of various tools to help measure aspects of your PHP code. will run the phploc tool on your current folder But… you can alias the tool, then simply run $ phpqa <toolname>…

  • k as in knife

    Many moons ago my earlier version of my blog had this list I use when spelling things for people over the phone. I managed to find it at archive.org and thought I’d repost… a as in aisle (or aye) b as in bdellium  c as in czar d as in djibouti e as in eight…

  • CyclopsMonitor

    I’ve been posting more about this new service over on linkedin, but haven’t posted much here. CyclopsMonitor is a web monitoring service – checking if a web address is up, how fast it responds, if specific content is available, when SSL/TLS certs expire, when domain name expires, and… sending you notifications when problems occur. Currently,…

  • Open Source TechFinder

    Inspired by the AUTM conference, I got inspired to look at some of the common processes techtransfer folks do. Main idea was to try to develop something relatively ‘standalone’ that might address a use case I learned about, so I decided on building a web-based open source techfinder tool to publish licensable technologies. The notion…

Leave a Reply

Your email address will not be published. Required fields are marked *