codewd
4 months ago

Generating JSON responses from collections extremely slow

Posted 4 months ago by codewd

We use Laravel (and love it) on several large scale web applications, and a couple of them are now getting some large amounts of data being returned in some queries, and also more specifically, some heavily nested relationships. We have optimised all the queries etc to run this efficiently and are getting query times from the debug bar that we are (mostly) happy with.

However, then attempting to return those collections from our API to the front end is horrendously slow. A complex object for example that takes 0.8-1.2 seconds to load from the database is then taking a further 11 seconds to convert the collection in to an array to be returned as json to the front end API call. And this collection has only 1 item in it (just with lots of nested relationships).

Does anyone else experience this issue? If so, how have you gone about solving it? Right now we are working through and finding that we may need to change a large number of things to do all the relationships as separate flat items that we then get as arrays rather than collections and stitch back together, so was hoping to see if anyone knew of a better way to optimise this? It feels like this is probably something the whole community would benefit from if we can work it out!

Thanks in advance for any ideas!

Please sign in or create an account to participate in this conversation.