5

Which is faster, to return ajax in JSON and then process JSON response to render the html, or just have the Ajax response the raw html in a bunch of <li></li>'s?

1
  • As others have said, it's probably not going to matter enough for this to be your deciding factor. IMO, you should choose JSON because it can be easily handled by any client for any purpose; not just a browser for the purpose of displaying. Commented Feb 28, 2012 at 18:43

3 Answers 3

5

Depends. In both cases, the server is simply returning a response with text. If the JSON version of the response requires more characters than the HTML version, that response will take longer to be transmitted back to the client, and vice versa.

But of course there is also the server-side script which must do its work. Perhaps in your case generating JSON is faster than HTML from your server-side script. No way for me to know.

And then there is the client-side processing. You'd have to parse the response to turn it into a true object, and then you'd need to iterate over the resulting object in order to generate the HTML. This will definitely take longer than just taking an HTML response and injecting it into the DOM.

However, I doubt that the performance difference will be noticeable, meaning that your decision about providing a JSON response vs. HTML response should be based on other factors.

Sign up to request clarification or add additional context in comments.

Comments

0

As already mentioned, that depends. From a server side point of view it makes a lot of sense to let the client generate the HTML because just serializing JSON is faster and takes a lot of strain off the server because it doesn't have to deal with all the HTML generation. An additional benefit is, that you offer an API when returning JSON that can be used for more than just outputting HTML.

If you want to take the work off the client it makes sense to generate the HTML on the server side.

In the end the speed of it depends a lot on the technologies used. Both ways can perform extremely well but when done wrong either one will be slow.

Comments

0

enter image description here

here as you can see i did the same response by HTML and JSON. the JSON response equals weight half what HTML response as kilobyte ,Thats means faster server-side respone. but in this case you have to rebulid the html from json so let calculate the json rebuild time and see

enter image description here

the first one is the html so it takes more time to do the server respone

now lets see appending it to html document

enter image description here

first one is html again the html proccess last longer than Json

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.