Preformance md-autocomplete slow with 3k items
Hi,
i'm having an autocomplete with around 3k items, but this is extreme slow, when looking in an AngularJS monitor tool;
I can see it created +30k watchers when loading in the element, so I would think there is something wrong
this is the array being generated:
locationService.query(function(locations) {
self.states = [];
angular.forEach(locations, function (l) {
if (!l.City) return;
var el = {
value: angular.lowercase(l.City),
display: l.City,
country: angular.lowercase(l.Country)
};
if (self.states.indexOf(el) === -1)
self.states.push(el);
});
console.debug(locations.length);
console.debug(self.states.length);
});
with the following html:
<md-autocomplete md-selected-item="header.selectedCityFrom"
md-search-text="header.searchTextCityFrom"
md-selected-item-change="header.addTag('from', 'city', header.selectedCityFrom.value)"
md-items="item in header.querySearchCities(header.searchTextCityFrom,header.searchTextCountryFrom)"
md-item-text="item.display"
md-min-length="0"
md-menu-class="autocomplete-custom-template"
placeholder="City">
<md-item-template>
<span class="item-title">
<span md-highlight-text="header.searchTextCityFrom" md-highlight-flags="^i">{{item.display}}</span>
</span>
<span class="item-metadata">
<span class="item-metastat">
{{item.country}}
</span>
</span>
</md-item-template>
<md-not-found>
No matching for "{{ctrl.searchText}}" were found.
</md-not-found>
</md-autocomplete>
I'd recommend having your server handle the lookup and only return a few results(around 10). Do you have any reason to keep the full dataset on your client?
If this is a client only app that doesn't already have a server you could try adding an optimized lookup function that acts like a server returning only a few results for md-autocomplete to look through. This would probably solve your watcher problem and it'd still let you keep your data set on the client.
+1
The problem isn't the amount of data, it's the idea that there are 10 watchers being created per item in the example provided by @cskiwi . 3000 items -> 30k watchers is over excessive, especially in browsers like IE10 where watchers provide a SIGNIFICANT slowdown of interface.
+1
+1
+1
Limiting the number of results returned from a query function wouldn't be ideal for our use case, as the vast majority of the results may share 90% of the same characters. Limiting the number of results returned from the query function to ten or so, would essentially ruin the functionality of the autocomplete in the first place.
Anyways, keep up the great work guys, angular material has quickly become my favourite ui framework when using angular.
What about using one-time bindings ?
<md-autocomplete md-selected-item="header.selectedCityFrom"
md-search-text="header.searchTextCityFrom"
md-selected-item-change="header.addTag('from', 'city', header.selectedCityFrom.value)"
md-items="item in header.querySearchCities(header.searchTextCityFrom,header.searchTextCountryFrom)"
md-item-text="::item.display"
md-min-length="0"
md-menu-class="autocomplete-custom-template"
placeholder="City">
<md-item-template>
<span class="item-title">
<span md-highlight-text="header.searchTextCityFrom" md-highlight-flags="^i">{{::item.display}}</span>
</span>
<span class="item-metadata">
<span class="item-metastat">
{{::item.country}}
</span>
</span>
</md-item-template>
<md-not-found>
No matching for "{{ctrl.searchText}}" were found.
</md-not-found>
</md-autocomplete>
@antoinebrault When I was using one time bindings in the md-autocomplete which now uses md-virtual-repeat it seemed to create a bit of weird behaviour, ex: I would select "Exterior", which would select the "Interior" object. Also, it's a virtual repeat now so it (I don't think) would create a speed increase as it already should be repeating the same DOM and just cycling through javascript vars.
I can neither confirm nor deny that there is an issue with excessive amounts of watchers being created, but as @epelc mentioned, limiting the amount of data that's actually used by autocomplete should be the way to go regardless of the issue with watchers.
The purpose of autocomplete is to help user to make a choice. Having more than 20 results presented to the user is simply not useful. If there are too many options, user needs to narrow down the search.
@DmitryEfimenko That simply is not feasible in our use case scenario.
Also, that's not the point of md-min-length="0", which is there to allow all results to be displayed.
@EsunaProxy
It's my understanding that md-min-length="0" determines how many characters user needs to type in the input before the suggestions start to show up. It does not determine how many results are to display. See api docs
I am very curious to hear what is your use case that would require so many suggestions to be displayed. To me, the more data is shown to the user the harder it is to comprehend it. I hope you can share details of your use case.
please move discussion to the forum. this is interesting but not a bug. :)
Thanks for this discussion... guided me to a few new directions for the issue I'm having:
- when in a ui using the md-autocomplete directive (inside md-chips) loading the md-autocomplete with a 2000 list (yes, we are considering to limit the items to a much smaller number) we see a 45 second delay before the list shows.
Using $$Watchers to see the number of watchers created, I see almost 21K watchers created.
The interesting thing is:
- If we FIRST, load the md-autocomplete up with a list of lets say 10 items... then load a 2000 item list. In this case, the 2000 list populates immediately... and a very very small number of watchers are created.
Thoughts? Ideas??? Any workarounds anybody has done to get around this issue?
Not easy to illustrate in a code-pen... but I can if anybody thing it's worth it.
Did this discussion move to a forum? If so, where do I find it???
Hmmmm.. that's a very interesting discovery, if the discussion has moved i am unaware of where it is, but tomorrow I will do some testing with what you said and see if I can make sense of it.
My solution was to limit the items displayed so far, but upcoming we will be unable to do this easily for all our clients who have massive data and request to display it all...
Limiting the number of items in the initial list, as @jornncc mentioned, does seem to help.
I was using md-min-length="0" to display ~2000 results on focus so users had the ability to scroll through the items just like a dropdown. Typing to filter that list was very laggy. When I updated the function that returns the list to only return the first 5 items when the search text is empty, the lag went away. As a workaround, I return the entire list if(searchText == "-") so users can type a single dash to get the full list to scroll through. It does not seem to slow down after loading the full list as long as the first list was small. It's not a great solution, but acceptable for my current use case.
A better workaround could probably be created using a timeout or callback to initially return a small list and then refresh it with a larger list.
@jornncc @mrmoses Do you have a sample implementation of the trick @jornncc mentioned?
@nikhilkulkarni Here's a sample:
<md-autocomplete md-selected-item="selectedItem"
md-search-text="searchText"
md-items="itemVal in ctrl.getAutoCompleteValues(searchText)"
md-item-text="itemVal"
md-min-length="0"
md-autoselect="true"
md-no-cache="true">
<md-item-template>
<span md-highlight-text="searchText" md-highlight-flags="^i">{{itemVal}}</span>
</md-item-template>
</md-autocomplete>
(using TypeScript)
public getAutoCompleteValues = (searchText: string) => {
var results: Array<Models.IAttributeValue> = [];
if (!searchText) {
// only return the first 5 initially, so the first time the autocomplete is filled, it'll only have a few to bind
results = this.allTheThings.slice(0, 5);
}
else if (/^-$/.test(searchText)) {
// allow getting all values if they type just a "-"
results = this.allTheThings;
} else {
searchText = _angular.lowercase(searchText);
results = this.allTheThings.filter((val) => {
return _angular.lowercase(val).indexOf(searchText) === 0;
});
}
return results;
}
Awesome! Many Thanks! 🙌