Ajax HTML Vs Xml/JSON Responses - Performance or Other Reasons

What are the advantages and disadvantages of json vs xml for ajax requests?

In summary, JSON (which can be thought of a subset of JavaScript) is a lot leaner than XML. This has several positive side-effects

  • JSON is smaller than corresponding XML
  • JSON is faster, i.e. simpler syntax -> easier parsing (faster parsing)

In my original answer to this question, my view of JSON was that of JavaScript, I considered it to be a close relative. But JSON is something independent and JSON.org does a great job of describing JSON. It's also provides a compatibility library for JavaScript that adds support for JSON.parse and JSON.stringify when not supported by browsers.

While eval at the time (mid 2009) was used to evaluate JavaScript, it could also evaluate JSON, i.e. parse JSON, but it was considered unsafe, as it did allow arbitrary JavaScript to execute in its stead.

JSON just happens to be a very good fit for browsers and a natural way to evolve the platform due to its close relationship with JavaScript.

While XML might be considered to have better rigor due to the fact that you can type it, it is also those things that make it a lot slower (it is also a bit verbose in my opinion). But if this is something you really want, you should use it, XML is equally ubiquitous.

I will not go into a debate over dynamic or statically typed, but I will say this. It's really easy to add stuff on top of schema-free data and there are plenty of ways to do validation, regardless of schema or no schema.

Performance comparison of AJAX requests: XML vs. JSON

You may check this video to see how big (huge, in fact) the problem of rendering html in modern browser is. Truly, you would save innocent users hundreds of hours of waiting.
http://www.youtube.com/watch?v=nCgQDjiotG0

Well, quit kidding now. The whole idea of switching to json because of speed sounds a bit extreme to me. Not because html has some technological advantages, but because generating html from templates is easy in every web framework (MVC, at least). It's what web frameworks do. And putting data into template in Javascript sounds bit tricky to me. Unnecessary complications.

Using XML for Ajax response vs normal HTML

I think this depends on where you are returning to. Usually if returning to javascript (which i am assuming here) is is beneficial to re turn using JSON format. JSON is simple and lightweight. There are methods in .NET and also jQuery that can parse and serilize/decerlize this.

jQuery.parseJSON( "json" ) 

this will parse a JSON string to a JQuery object

JSON.stringify(your_object) 

this will parse an object into a jquery string you can send up to an ajax call

using System.Web.Script.Serialization;
IList<NewFee> ExistingNewFees = new JavaScriptSerializer().Deserialize<IList<NewFee>>(ExistingNewFeesJSON);

this snippet illustrates deserlizing a json string into a generic list type of a predefined class.

When creating an AJAX call - what kind of data do I need to ask for?

It really depends. Each method has its pros and cons.

  • JSON

Pros: smaller size, interoperable

Cons: more difficult to build UI on the client compared to HTML although there are now client side templating frameworks that could help you.

  • HTML

Pros: very simple on the client, practically a single line of code: $('#foo').html(result);

Cons: non interoperable. If your client is a WPF application for example you will have parse this HTML to extract the information you are interested in in order to build your UI.

So depending on your requirements and environment you might decide to choose one or the other approach for your application.

Javascript - Json or html?

If you want the user be able to switch between different views, and this is data that can simply be included on page load, then just include onload then hide/show or load as json and then populate as needed.

If this is a very simple request that needs to be updated as needed and you don't plan on other similar needs in your app then just return html.

However if you are making a request that submits data or performs some action, I am a fan of returning a consistent json response with error boolean, error message and content/data elements.

AJAX - Using JSON vs echo HTML

I think the JSON method is better, since there's less data to transfer for consecutive requests.

If you're really that conscious about performance, you should precompile your templates.

Additionally, if you precompile your templates, you don't even have to send handlebars.js to the client. You can send a much smaller handlebars.runtime.js to the browser, further decreasing load time!

Why is Everyone Choosing JSON Over XML for jQuery?

Basically because JSON is recognized natively by JavaScript, it's really lightweight, minimalistic and highly portable because it relies only on two fundamental structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

Performance considerations of JSON vs. XML

"PHP or JavaScript" sounds like an odd choice to offer: PHP is usually used as a server-side language, whereas JavaScript is usually used as a client-side language.

What's your situation? What makes you suggest those two languages in particular? If you could give more information about what you're trying to do, that would help a lot. (We don't know whether you're developing a web app, a batch processing tool, a GUI application, etc.)

I suspect JSON will be a bit more compact than XML, although if they're compressing the data you may well find they end up taking the same bandwith (as a lot of the "bloat" of XML is easily compressible).

As ever, the best way to find out is to test the specific web service with some realistic data. Generalities aren't a good basis for decision-making.



Related Topics



Leave a reply



Submit