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

  • Onboarding freelancers

    Maari Casey over at uncompany had a recent LinkedIn post about planning for freelancers. She made some good points, but I think skipped one, and it’s not just relevant for freelancers. Even well before an organization might need extra work – be it freelance or employee – companies need to have a plan for onboarding….

  • Bad I9 PDF form

    Have been needing to programmatically fill out an I9 PDF, retrieved from gov site. Should be fairly straightforward, right? Well… the field names are… a mess. Field names like topmostSubform[0].Page1[0].U\.S\._Social_Security_Number__Last_4_numbers_[0]topmostSubform[0].Page1[0].expiration_date__if_applicable__mm_dd_yyyy[0] topmostSubform[0].Page2[0].Employers_Business_or_Organization_Address_Street_Number_and_Name[0] and so on make it pretty… not straightforward to create a usable key/value combination to search and replace. But… today, I noticed it got…

  • chunked file uploads with plupload

    Holy tamole… Have been wrestling with a client project using ‘plupload’ with a user base consistently uploading files from 150-500meg on a daily basis. They’d been uploading to youtube/vimeo mostly, because the experience with the older uploader (still plupload earlier version) was bad – slow, mostly, but some issues about determining whether items were uploaded/processed…

  • PHP assert not working in Laravel with Sail?

    Recently, I hit a weird ‘bug’. The $file was not being created, so I’d added a quick ‘assert’ in the code, and… file_exists($file) was indeed false (checked when stepping through debugger), but assert was not stopping/failing. But… running a simple assert from command line was working. This was something different only when running under the…

Leave a Reply

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