

Ask HN: One ajax call or two simultaneous calls? Which is faster? - SingAlong

I have to get an image and comments related to it. Now I have an option of making one ajax call and getting an html page with the iage and comments or get the image and comments in two simultaneous ajax calls. Which option is faster or better for the user?
======
pedalpete
I was considering a similar issue not long ago and decided on one call for the
following reasons 1) user percieved speed - I can't control the users
bandwidth, or memory, and two ajax calls means two pages being retrieved by
the users browser, and then two different functions to place those pages into
the users browser 2) error handling - what if there is an error with the image
or comments? the user will get one portion returned, and not the other (minor
disconnects happen all the time and usually it isn't detrimental, but the more
ajax calls, the more opportunity for failure).

In the end I decided to do one ajax call and the split the returned data into
two strings and place them on the page.

You can check out the results at www.HearWhere.com

Also, if you are always getting the image with related comments, I would
suspect that they would both go within the same area/div of a page, so why
split them?

------
boucher
I guess I don't really understand the question. You're loading text based
content, and an image, and you want to know whether or not to get everything
in one XMLHTTPRequest, or to load two separate XMLHTTPRequests?

Why do you need a request at all for the image? If you've got the information
you need and predictable URLs, you could use one XMLHTTPRequest, and
simultaneously create an image in JavaScript and set it's source, right? This
will obviously be faster than first going to your server, then creating the
image in the resulting html. Of course, if you can't actually generate the
image tag without hitting your server, then this is all moot.

------
noodle
my rule of thumb, that i would apply to this situation, is _keep it simple_.
if you want to optimize, test both options in your own app. i don't think that
one can generalize this situation to say for sure which option is faster or
better without also having the context of your application in mind.

