I want to know which is faster: XML and JSON?
When to use which one ?
I want to know which is faster: XML and JSON? When to use which
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Before answering when to use which one, a little background:
edit: I should mention that this comparison is really from the perspective of using them in a browser with JavaScript. It’s not the way either data format has to be used, and there are plenty of good parsers which will change the details to make what I’m saying not quite valid.
JSON is both more compact and (in my view) more readable – in transmission it can be “faster” simply because less data is transferred.
In parsing, it depends on your parser. A parser turning the code (be it JSON or XML) into a data structure (like a map) may benefit from the strict nature of XML (XML Schemas disambiguate the data structure nicely) – however in JSON the type of an item (String/Number/Nested JSON Object) can be inferred syntactically, e.g:
The parser doesn’t need to be intelligent enough to realise that
12represents a number, (andDanielleis a string like any other). So in javascript we can do:In XML we’d have to do something like the following:
(as an aside, this illustrates the point that XML is rather more verbose; a concern for data transmission). To use this data, we’d run it through a parser, then we’d have to call something like:
Actually, a good parser might well type the
agefor you (on the other hand, you might well not want it to). What’s going on when we access this data is – instead of doing an attribute lookup like in the JSON example above – we’re doing a map lookup on the keyname. It might be more intuitive to form the XML like this:But we’d still have to do map lookups to access our data:
EDIT: Original:
This is misleading: remember that in JavaScript (and other dynamic languages) there’s no difference between a map lookup and a field lookup. In fact, a field lookup is just a map lookup.
If you want a really worthwhile comparison, the best is to benchmark it – do the benchmarks in the context where you plan to use the data.
As I have been typing, Felix Kling has already put up a fairly succinct answer comparing them in terms of when to use each one, so I won’t go on any further.