Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it better or group my ajax requests or send every request separately?

I'm developing an ajax project, but i'm confusing in something

i have 3 functions that are sending data to the server every function has a certain job.

is it better to group the ajax request that will be send from each function and send it in one big request. which will reduce my requests count & time. or send each request separately which will reduce execution time for my code at the server side but will make many requests.

P.S: for my application it works in both cases.

like image 958
trrrrrrm Avatar asked Nov 06 '22 16:11

trrrrrrm


2 Answers

If you can consolidate them into a single request, neatly, I'd suggest you do that. Browsers will put restrictions on how many requests you can be making simultaneously (can be as low as 2), as will many servers.

Of course, as always, there are exceptions. If you have some data that you need to query every 10 seconds, and other data you need to query every 10 minutes, it's really not necessary to query the 10-minute data along with the 10-second data every 10 seconds.

Perhaps a single request with options that control what is sent back. You can request large amounts of data, or small amounts of data, all from the same request depending on the options you pass along with the request itself.

Just theory.

like image 58
Sampson Avatar answered Nov 12 '22 16:11

Sampson


There are several factors involved here.

  • Putting them together on the client has the advantage of the client doing more of the work, which is good.
  • All but the newest browsers only allow 2 requests to the same domain at a time. If you have a lot of latency, this would cause to to want to group requests together if they're going to the same domain. However, newer browsers allow 8 requests at a time, so this problem will gradually go away.

But on the other hand

  • Do the requests need to all be made at the same time? Keeping the requests separate will allow you to maintain flexibility.
  • Probably the most important: Keeping the request separate will probably result in more maintainable, straightforward code.

I would suggest doing whatever keeps the code on the browser straightforward. Javascript (or frameworks that do ajax for you) is difficult enough to maintain and coordinate. Keep it simple!

like image 30
Patrick Karcher Avatar answered Nov 12 '22 16:11

Patrick Karcher