Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance considerations of JSON vs. XML

I am using a webservice which provides a large result set either in XML or JSON format. Which format will be faster or better (perfomance based)? Also which language should I use to parse the XML/JSON? Should I use PHP or JavaScript?

like image 797
Andromeda Avatar asked Jun 10 '09 08:06

Andromeda


People also ask

How much faster is JSON than XML?

JSON is very simple, and parsing is fast compared to XML (in some cases up to 100x faster). Conceptually, a JSON file is made up of two types of structure: a list of name and value pairs, and an ordered list of values.

Is XML more size efficient than JSON?

First, as previously mentioned, while XML is a markup language, JSON, on the other hand, is a data format. One of the most significant advantages of using JSON is that the file size is smaller; thus, transferring data is faster than XML.

When would JSON be preferred and when would XML be preferred?

Usually JSON is more compact, and faster to parse. Prefer XML if: You need to process the data on the client, and you can leverage XSL for that. Chances are the XML + XSL chain will work faster than JSON + JavaScript especially for big chunks of data.

What are the advantages of JSON over XML?

There are 3 commonly discussed benefits of JSON over XML: In most scenarios, JSON is undoubtedly easier to read in its expanded form than XML. JSON can have a substantially lower character count reducing the overhead in data transfers. JSON is much easier to parse.


2 Answers

"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.

like image 174
Jon Skeet Avatar answered Sep 22 '22 12:09

Jon Skeet


both have their advantages:

JSON

  • easy to handle: $dataStructure = JSON_decode($serializedString);, done.

XML

  • partial data handling: if your result-set is too big to be processed (parsed) at once, this may be the way to go. note: SimpleXML is the easier to work with xml lib, but also parses the whole xml-file at once, so in this case there's no benefit over JSON.

the question which language to handle your result set with is a bit non-sensical. javascript is client-side*, php is server side. so, it depends on what you want to do with the result set.

you can pass the result directly on to the browser/js without doing anything on the server side, and let the client do the filtering and rendering. this may make sense in certain situations, but normally it's not what you want.

my advice: if possible, use JSON.

ad *: you can use javascript on the server side (rhino, v8cgi, ...), but that's not what you have in mind.

like image 45
stefs Avatar answered Sep 21 '22 12:09

stefs