Khirad Zahra
Khirad Zahra

Reputation: 893

Too many request to server using jquery autocomplete function

Suppose i have thousands of items in database,and i want to make them searchable using some sort of autocomplete like jQuery autocomplete,but i don't want to use ajax as with each key press a request will be made to database.

I can use this one without ajax.

$( function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++"
    ];
    $( "#tags" ).autocomplete({
      source: availableTags
    });
  } );

But if as i have thousands of items,this will also impact on server and website speed ?

Is it wise able to first fetch all records and place them in a seprate file and then search it form there,in order to make website more speedy and less load on the server?

Upvotes: 0

Views: 670

Answers (1)

Elias Soares
Elias Soares

Reputation: 10254

If you have thousands of records, of course you will need use ajax, otherwise you will be loading a lot of data on client-side, exposing the entire information to someone that want's to steal it, also slowing down the page loading.

The are two ways to reduce requests:

Implement a debounce

A debounce is just a timer, that wait X milliseconds before doing the request. If the user types something before this timer finish, you cancel it and start another.

Downside: It needs a fine tuning on the debounce time so user don't feel some lag.

Cancel the requests

You can trigger a request on every key press, but if another key is pressed, you cancel the previous request. This may reduce the load on server, since the requests may not be finished yet.

Downside: It only reduces the load on server if the user type faster then server response time.

Combining Debounce with Cancel Request

If you combine both the techniques above, you will have a good result, because you can reduce the debounce time to have an imperceptible delay, and also cancel the pending requests if user types slowly.

Upvotes: 2

Related Questions