I am having an application developed with pure JSP and servlet. In one of my JSP pages, I have a field where it should auto complete when the user start typing. For this purpose, I used JQuery Auto Complete (https://jqueryui.com/autocomplete/) pluging.
What I do is, I load data from my database using a Servlet and pass that data to the JSP as a Bean
. In my JSP, I use JSTL inside JavaScript for feeding the auto complete field.
Below is the code
<script>
$(function () {
var availableTags=[] ;
<c:forEach var="NamesList" items="${requestScope['NamesList']}">
availableTags.push("${NamesList.Name}");
</c:forEach>
$("#addTxt").autocomplete({
source: availableTags
});
$( "#addTxt" ).autocomplete( "option", "appendTo", ".form-horizontal" );
});
</script>
<input id="addNameTxt" name="nameTxt" class="form-control input-md" >
But here is the best part. This list contains 22,500 elements. Some are long names, which could contain more than 50/60 words. Now the issue is, eventhough it do not take much time to load the page, "sometimes" it get stuck when the auto complete starts. I guess it is searching for lot of elements and gets out of memory or something.
I also noticed the below behavior, which might cause this lag. Imagine I have texts like below.
Cat/Bat/Dog/Space ship/FrontLine
Cat
Bat
Space Ship
If I type "Bat", my expectation is to find text starting with "Bat", and not texts which may contain "Bat" somewhere in the middle. Since this kind of search taking time, I guess it is getting laggi as well, when there are 22,500 records. Anyway, that is just a side note.
So, how can I fix this issue and make the auto complete fast?