JSON and XML performance issues

I use webservice, which provides a large set of results in XML or JSON format. Which format will be faster or better (based on performance)? Also, which language should be used for parsing XML / JSON? Should I use PHP or JavaScript?

+4
source share
8 answers

"PHP or JavaScript" sounds like a weird choice: PHP is usually used as a server language, while JavaScript is usually used as a client-side language.

What is your situation? What makes you offer these two languages ​​in particular? If you could give more information about what you are trying to do, this will help a lot. (We do not know if you are developing a web application, batch processing tool, graphical application, etc.).

I suspect that JSON will be a little more compact than XML, although if they compress the data, you may well find that they end up taking the exact same bandwidth (since most of the "bloat" XML is easily compressed).

As always, the best way to find out is to check a specific web service with some realistic data. General provisions are not a good basis for decision making.

+11
source

both have their advantages:

Json

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

XML

  • partial data processing: if your result set is too large to be processed (analyzed) immediately, this may be the way to go. note: SimpleXML is easier to work with xml lib, but at the same time parses the whole xml file, so in this case there is no benefit from JSON.

the question of which language to process your result set is a little insensitive. javascript - client side *, php - server side. therefore, it depends on what you want to do with the result set.

you can pass the result directly to the / js browser without doing anything on the server side, and let the client do the filtering and rendering. this may make sense in certain situations, but usually this is 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 mean.

+13
source

I would go for JSON, you do not pay "bracket tax". The choice between PHP and Javascript is related to the amount of processing required for the data (I am doing the jump here).

A lot of processing, use PHP so that the server side. A little processing uses Javascript for a more responsive page (loading data via AJAX).

+2
source

I think you have to measure it yourself. Performance will depend on:

  • data size
  • data complexity
  • its format (JSON or XML)

So you can see that there are a number of variables.

Does your web service use more time to collect data in one format than another?

There are a large number of options for parsing JASON and XML, so do not limit yourself to PHP or Javascript (if you have a choice). And finally, if you request from a web service, you will have overhead for the transport network, connection setup, etc. Thus, any time savings in performance analysis can be negligible. Your efforts may be better spent elsewhere.

I don’t think I answered your question, except to give you more to think about!

+1
source

Although performance aspects vary greatly between language / tool combinations, at the end of xml and json tend to have similar characteristics when using the best platform tools. That is, you will not find more than once twice as fast or more; theoretical limits are similar for text formats. Both are "good enough" in this regard for almost any use case.

Therefore, I would focus more on supporting the tool for your task. In addition, format selection is unlikely to be the most important aspect to consider.

And, as John said, comparing PHP and Javascript really sounds weird ... apples and oranges or so.

+1
source

One thing that may have been overlooked is that the JavaScript client does not need to parse JSON, so you get a performance gain. The browser will parse the XML in the DOM and pass it on to your callback, then you need to extract the information you need from this DOM. With JSON, you are returning an object, and there is no need to extract from the DOM.

+1
source

You should use python, preferably :)

As for the format, I think JSON will be a little faster, but it depends on the details, and this task will in any case be connected with the network.

-1
source

If you use the application with ajax, then you should choose Javascript to process data on the client side, which will reduce the use of php on the server, which means a more efficient server side. you can save your result set in a json file and then call it on the client side using javascript, this will be the best possible way, since it will not consume resources on the server and the data will be processed on the client side. here i prefer json over xml because xml takes up more storage space than json due to its tag style and json is like an array in javascript which will be faster than xml. same thing on server side, you can easily convert your json array to php array (just call the json_decode function in PHP).

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

to improve performance, you should reduce server-side data processing and use a client-side resource, and this approach will give you the speed and cost-effectiveness of your application.

-3
source

Source: https://habr.com/ru/post/1285996/


All Articles