I am engaged in a small debate with the server side developer for my project (I’m the front end guy) around injecting new stuff into DOM. He maintains that the best method to inject a large amount of code (received via ajax) is to send a JSON object and then to iterate through each item of that object. He says that will save some bandwidth and is more server friendly.
Obviously (for me, at least 🙂 ), this mean a LOT of cpu cycles on client.
The data is basically a table with 20-30 rows (2-3 cols each), which means few (useless) iterations.
On the other hand, I think that the best method is to send pure XHTML (server generated source) and just inject it in place. This means just one cpu cycle ($('selector').html(data) where data is the data received with AJAX, but also means a lot of bloated HTML code.
I use jQuery (but I think that is not too important).
So, what do you think, guys? Thanks!
Even as at first I commonly send
HTMLto be inserted at specific places, now I tend to useJSONwhenever I can,XMLotherwise, mainly because I can modify a lot of places of the page, and act more dynamically with little information.Also,
JSONhas a smaller footprint thanXMLand is also usually better "human parseable". XMl’s biggest advantage is that it has been around for a long time and is the standard, so you have both the tools, and the knowledgeable workforce at hand for it. JSON, on the other hand is a little more obscure. Of course, any developer worth its paycheck will be able to learn it faster than I can write this. Just think about it:About just sending
HTMLQuirksmode says:Furthermore, by sending just HTML you can just paste information, you won’t be getting information, you’d be getting snippets, so you couldn’t operate with it. Remember that the advantage of AJAX is to have dynamic pages, not pages that have some parts that update without reloading the full page. You can use it for that, and is OK, and a valid use, but you are under utilizing the potential.
Even when inserting HTML could be faster than
dommanipulation, I don’t think that its that much (besides the problems you could have with IE 6). This you should test and see if for your use, its really a bottle neck of performance.I tend to agree with the closing argument of the previous link.