我的应用程序中有以下jquery自动完成功能(https://github.com/devbridge/jQuery-Autocomplete)
但是它的渲染速度非常慢,几乎需要4秒。我安装了debugbar,时间线显示启动耗时近3秒,应用程序耗时1秒,实际数据库查询耗时44毫秒。
这是我的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);
}
为了记录在案,我在Homestead上用php7.4在Windows10机器上运行这个
有什么想法吗?我该如何调整它,因为目前它不太可用?
可以通过限制记录和限制您正在获取的数据来完成一个小的改进,如下所示-
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&用于预填充的名称字段
获取较少的数据量是一个小技巧,我希望它能提高一些性能。
所以我安装了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