在 Laravel 中,为什么 "return response()->json([])" 比 "return []" 多花 700 毫秒

In Laravel, why does "return response()->json([])" take 700 ms longer than "return []"

在 Laravel 中,我可以从这样的控制器 return:

return response()->json(["name" => "Bob"])

或者我可以这样return:

return ["name" => "Bob"]

这两个 return 是同一件事,但第一个需要大约 700 毫秒的时间。有人知道为什么以及如何解决这个问题吗?

额外详细信息:Laravel 版本 8。我在笔记本电脑上使用 Sail (Docker)。

更多详细信息:我使用 Chrome 开发人员工具测量了请求的时间。我在两种不同的方式之间来回反复多次,结果一致。

如果您想查看该项目,这里是它的精简版:https://github.com/tkoop/slow_laravel_response

检查您的项目后,包 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。你可以 运行 php artisan icons:cache (它仍然会减慢 ~100ms 的响应速度)或者禁用 Blade 组件(没有明显的减慢),如果你不需要它们:

php artisan vendor:publish --tag=blade-icons

# /config/blade-icons.php
return [
    'components' => [
        'disabled' => true,
    ],
];