Skip to main content

How to improve performance of Jquery autocomplete



I was planning to use jquery autocomplete for a site and have implemented a test version. Im now using an ajax call to retrieve a new list of strings for every character input. The problem is that it gets rather slow, 1.5s before the new list is populated. What is the best way to make autocomplete fast? Im using cakephp and just doing a find and with a limit of 10 items.





Source: Tips4all

Comments

  1. This article - about how flickr does autocomplete is a very good read. I had a few "wow" experiences reading it.


    "This widget downloads a list of all
    of your contacts, in JavaScript, in
    under 200ms (this is true even for
    members with 10,000+ contacts). In
    order to get this level of
    performance, we had to completely
    rethink how we send data from the
    server to the client."

    ReplyDelete
  2. 1.5-second intervals are very wide gaps to serve an autocomplete service.


    Firstly optimize your query and db
    connections. Try keeping your db connection
    alive with memory caching.
    Use result caching methods if your
    service is highly used to ignore re-fetchs.
    Use a client cache (a JS list) to keep the old requests on the client. If user types back and erases, it is going to be useful. Results will come from the frontend cache instead of backend point.
    Regex filtering on the client side wont be costly, you may give it a chance.

    ReplyDelete
  3. The real issue for speed in this case I believe is the time it takes to run the query on the database. If there is no way to improve the speed of your query then maybe extending your search to include more items with a some highly ranked results in it you can perform one search every other character, and filter through 20-30 results on the client side.

    This may improve the appearance of performance, but at 1.5 seconds, I would first try to improve the query speed.

    Other than that, if you can give us some more information I may be able to give you a more specific answer.

    Good luck!

    ReplyDelete
  4. Before doing some optimizations you should first analyze where the bottle-neck is. Try to find out how long each step (input → request → db query → response → display) takes. Maybe the CakePHP implementation has a delay not to send a request for every character entered.

    ReplyDelete
  5. Autocomplete itself is not slow, although your implementation certainly could be. The first thing I would check is the value of your delay option (see jQuery docs). Next, I would check your query: you might only be bringing back 10 records but are you doing a huge table scan to get those 10 records? Are you bringing back a ton of records from the database into a collection and then taking 10 items from the collection instead of doing server-side paging on the database? A simple index might help, but you are going to have to do some testing to be sure.

    ReplyDelete
  6. Try preloading your list object instead of doing the query on the fly.

    Also the autocomplete has a 300 ms delay by default.
    Perhaps remove the delay

    $( ".selector" ).autocomplete({ delay: 0 });

    ReplyDelete
  7. Don't use PHP/SQL.
    My autocomplete written on C++, and uses hashtables to lookup.
    See performance here:

    http://olegh.ath.cx/autocomplete.html

    This is Celeron-300 computer, FreeBDS, apachie... And, you see, runs quick on huge ditcionaries...

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex