Efficiency Dilemma: Use AJAX or Not?

I have a website that displays a list of articles, as well as category filters. Filters can be turned on and off, and the list of articles will automatically change.

I currently have this, so when I click on filters, AJAX makes a request to a script that queries the database based on the selected filters, then the results are displayed to the user.

Is this an inefficient method? Another idea I recently got was to simply query the database for EVERY article once when the page loads. Then, instead of making AJAX requests, the filters will switch accordingly with the display element of each message.

This sounds a lot better than using AJAX, but I wanted to get other opinions on the method of efficiency and semantics.

+4
source share
4 answers

It really depends on how many articles you have. If there are hundreds or thousands of them, you will probably save processing by fetching over AJAX. Otherwise, client-side filtering makes more sense.

+2
source

It all depends on your specific numbers. How many visitors do you have? How hard is it to load the whole page? How many seconds increases the page load to download all articles.

Generally speaking, if you do not have many articles, I suggest downloading them all at once, the user benefits from faster downloads, and your life is easier because you do not need to write a specific service to download articles one at a time.

This suggests that your ajax solution is not inefficient, and it is the best solution if the number of articles increases.

+2
source

I think it depends on how many lines you are trying to cache. If it is less than 1000, I would probably cache it, but if it is more than you can use AJAX. AJAX is ineffective in general due to restrictions on moving data over http.

+1
source

How do you execute this request? GET with parameters?

If so, you can read about varnish for caching it. Don't worry about that anymore.

The first request will be cached in memory. Others will never touch the DB or script file, but just a cached response.

Configure the varnish for this after 2 - 2 minutes and get a high-performance request (even if millions will request this AJAX, only one request will be executed in the database)

0
source

Source: https://habr.com/ru/post/1379572/


All Articles