Returning data to client-side autocomplete query
I'm using a JQuery plugin (http://docs.jquery.com/Plugins/Autocomplete) to add auto-completion to a "city" textfield. The component calls an ASP.NET page that simply loads an array of all possible city values (>8000) and then iterates that array returning those that start with the text the user has so far entered.
The thing is, it's pretty slow in real use. It lags behind what the user types to the extent that most of the time the user probably won't notice that it's there.
So, my question is, how can I speed it up?
I had thought that an array would be a better way to go than putting the data in a database and having to hit that multiple times. Do others agree that having this information hard-coded is the way to go given that it's not at all volatile and needs to be all about speed of return?
If so, what would you look at to improve the speed of performance? Should I be caching the data on application start and accessing it from memory? Would I be better off instead with multiple arrays, each containing values starting with a particular letter so I can go straight to the relevant one and thus开发者_JAVA百科 iterate a much smaller array? Or am I missing a much more obvious way to go about this?
Thanks in advance.
The code you posted looks pretty normal and should return fast. My guess would be that you're using the default "delay" value for the plug-in which is 400ms. Try changing it to something lower like 100ms (or even lower) and see if it feels better:
$('#textbox').autocomplete('your_url_here', {
delay: 100
});
Well you probably don't want to hard code any data if you can avoid it. Having it in a database will make it much easier to manage changes. And there's no reason you would have to query the database each time. You could cache data from sql in memory into multiple lists as you touched on yourself. That should be quite speedy, unless there's some sort of additional network latency problem. You might want to post some of your code so people can provide more specific guidance.
Thanks for the reply. (Very basic) code is as follows:
Dim q As String = LCase(Server.UrlDecode(Request.QueryString("q")))
Dim arrCities() As String = {"Abano Terme", "Abbadia Cerreto", ... "Zungri"}
For Each s As String In arrCities
If s.ToLower.StartsWith(q) Then
Response.Write(s & vbCrLf)
End If
Next
Line 2 is basically a huge array declared right in the code. It isn't pretty, but the data is involatile so I thought I could get away with it and I assumed it would be faster than pulling it directly from the database. But maybe SQL with some caching if you can offer some specific advice?
精彩评论