Laravel ajax自动完成非常慢

问题描述 投票:1回答:2

我在我的应用程序中具有以下jquery自动完成功能(https://github.com/devbridge/jQuery-Autocomplete

但是速度非常慢-渲染需要将近4秒钟。我已经安装了debugbar,时间轴显示启动过程几乎耗时3s,应用程序耗时1s,实际数据库查询为44ms。

这是我的jQuery自动完成实现:

window.locationAutoCompleteSettings = {
    serviceUrl: "/location-lookup",
    minChars: 2,
    paramName: "term",
    showNoSuggestionNotice: false,
    onSearchStart: function (query) {
        $(this).closest(".js-clear-input").find(".js-clear-input-trigger").addClass("clear-input-spinner")
    },
    onSearchComplete: function (query) {
        $(this).closest(".js-clear-input").find(".js-clear-input-trigger").removeClass("clear-input-spinner")
    },
    transformResult: function (response) {
        var data = JSON.parse(response);
        return {
            suggestions: $.map(data, function (item) {
                return {
                    value: item.name,
                    data: item.id
                }
            })
        };
    },
    onSelect: function (suggestion) {
        var e = $(this).closest(".js-lookup-container").find(".js-location-id");
        e.val(suggestion.data);
        var f = suggestion.value, b = $(".js-location-description");
        if (b.length && b.val() == "") {
            b.val(f).trigger("change")
        }

    }
};

$(".js-location-lookup").autocomplete(window.locationAutoCompleteSettings);

这是我的控制器方法:

public function locationLookup(Request $request)
{
    $term = $request->input('term');

    $locations = $this->locationRepository->getByName($term)->get();

    return response()->json($locations);
}

为了记录,我正在Windows 10机器上的php 7.4上在Homestead上运行它

任何想法,我怎么能对此进行调整,因为就目前而言,它不是很有用?

php ajax jquery-ui-autocomplete laravel-6
2个回答
0
投票

可以通过限制记录和限制要获取的数据来进行小幅改进,如下所示-

public function locationLookup(Request $request)
{
    $term = $request->input('term');

    $locations = $this->locationRepository->getByName($term)->take(10)->get(['id', 'name']);

    return response()->json($locations);
}
  • 这里假设您一次只预填充了10条记录,用于自动建议
  • 并且您仅需要id和name字段即可进行预填充

读取少量数据有点麻烦,我希望它可以提高性能。


0
投票

所以我已经安装了https://github.com/winnfsd/vagrant-winnfsd

$ vagrant plugin install vagrant-winnfsd

如下更新Homestead.yaml:

folders:
- map: ~/code/project1
  to: /home/vagrant/project1
  type: "nfs"

运行

vagrant up --provision

现在启动已降低到500ms

© www.soinside.com 2019 - 2024. All rights reserved.