Performance considerations of JSON vs. XML

2020-06-21 19:02发布

问题:

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?

回答1:

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



回答2:

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.



回答3:

I would go for JSON, you're not paying the "angled bracket tax". The choice between PHP and Javascript is related to the amount of processing required on the data (I'm taking a leap here).

Lots of processing, use PHP so it's server side. Little processing use Javascript for a more responsive page (load data via AJAX).



回答4:

Although performance aspects really vary a lot between language/tool combinations, in the end xml and json tend to have similar performance characteristics when using best tools of the platform. That is, you won't find one more twice as fast or more; theoretical limits are similar for textual formats. Both are "good enough" in this regard for almost any use case.

So I would focus more on tool support, for the task you have. Other than that, format choice is unlikely to be the most important aspect to consider.

And like Jon mentioned, comparison of PHP and Javascript really sounds odd... apples and oranges or so.



回答5:

One thing that has perhaps been missed is that a JavaScript client does not have to parse JSON, so you will get a performance win there. The browser will parse the XML into a DOM and hand this to your callback, you then need to extract from that DOM the info you need. With JSON you get back an object and the need to extract from the DOM is gone.



回答6:

I think you'll have to measure it yourself. The performance will depend on:

  1. the size of the data
  2. the complexity of the data
  3. its format (JSON or XML)

So you can see there are a number of variables.

Does the web service that you're using take longer to assemble the data in one format vs. another ?

There are a sizable number of options for parsing JSON and XML, so don't restrict yourself to PHP or Javascript (if you have a choice). And finally, if you're requesting from a webservice, you'll have the overhead of network transport costs, connection setup etc. So any time savings in parsing performance may be negligible. Your efforts may be better spent elsewhere.

I don't think I've answered your question, other than give you more things to think about!



回答7:

You should use python, preferably :)

As for format, I guess JSON would be a bit faster, but it depends on the details, and this task would be network-bound anyway.



回答8:

If you are using application with ajax then you should choose Javascript to process data on the client side which will reduce the usage of php on the server means more efficient server side. you can store your resultset in a json file and then can call it on client side with javascript this will be the best possible way because this will not consume you resources on server and data will be processed in the client side. here i will give preference to json over xml because xml takes more space to store than json because of its style of tags and json is like an array in javascript which will be faster than xml. same thing in server side, you can easly convert your json array to php array (just refer to json_decode function in PHP).

Now days json is in fashion because of it is easy to use and is faster.

for faster performance you should reduce the data processing on the server side and should use client side resource instead this approach will give your application speed and cost effectiveness.