'In Laravel, why does "return response()->json([])" take 700 ms longer than "return []"
In Laravel, I can return from a controller like this:
return response()->json(["name" => "Bob"])
Or I can return like this:
return ["name" => "Bob"]
Both of these return the same thing, but the first one takes about 700ms longer. Does anybody know why and how to fix this?
Extra details: Laravel version 8. I'm using Sail (Docker) on my laptop.
More details: I measured the time of the request with Chrome developer tools. I went back and forth a number of times, between the two different ways, with consistent results.
If you want to see the project, here is a stripped down version of it: https://github.com/tkoop/slow_laravel_response
Solution 1:[1]
Upon inspecting your project, the package codeat3/blade-google-material-design-icons is the culprit. It depends on blade-ui-kit/blade-icons and according to the readme file, the problem is caching. You can either run php artisan icons:cache (it will still slow down the response by ~100ms) or disable Blade components (no noticeable slow down) if you don't need them:
php artisan vendor:publish --tag=blade-icons
# /config/blade-icons.php
return [
'components' => [
'disabled' => true,
],
];
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Razor |
