PHP: is JSON or XML parser faster?

15,459

Solution 1

I didn't do any benchmark but...

Since JSON is only a description of nested string sequences, without the need to offer a DOM interface, attributes parsing and other subtle stuff, my guess is that a JSON parser is WAY faster that an XML parser.

Solution 2

I tend to find that simplexml_load_string() is faster than json_decode() when the json return is an object.

However, having the json returned as an array using json_decode($string, true) is actually a lot faster than using an object (as is true with most things PHP when comparing arrays to objects).

Ive seen json_decode() being over twice as fast as simplexml_load_string() under these circumstances.

Solution 3

JSON tends to be a lot smaller in size

Also you run json_decode once and then access the data as an array no use for any other functions.

Without running benchmarks id go with JSON is faster

Solution 4

Have you considered how overall bandwidth savings might affect the performance of your script? If your requests are going to be repeated a significant amount of time using JSON should be the sound economic choice.

Solution 5

The answer depends on how do you plan to use it. I mean if you will create some request and process them inside PHP script, I believe XML will be much faster. But once you considering to create and AJAX calls and later process of result you should consider using JSON, since you benefit from JavaScript automatic representation of JSON result as an objects as well it supports cross domain request with callback functions, whereas for XML you will proxy.

Share:
15,459
Admin
Author by

Admin

Updated on June 17, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm building classes that interface with the Twitter API, and I'm wondering whether PHP's built-in XML or JSON parser is faster? Twitter will send me the same data in either format, so PHP performance will determine my choice. I'm using php_apc, so you can disregard parse time and assume I'm running off bytecode.

    Thanks!

    more: I'm just looking to get associative arrays from the data. I'm not doing tree walking, node iteration or anything too complex. The format will always be the same. (I hope!)

  • Muhammad Huzaifa
    Muhammad Huzaifa almost 15 years
    I agree. JSON has a much simpler and less dynamic structure.
  • Artem Barger
    Artem Barger almost 15 years
    Parsing JSON format is much more complicated than well defined XML. Parsing string is very complicated job, maybe because you use standard library function you don't feel it, but is much simpler to parse structured XML than data structured in JSON.
  • instanceof me
    instanceof me almost 15 years
    If you consider only pure parsing, JSON should be way faster. But it also depends on the use of the parsed data.
  • StaxMan
    StaxMan almost 14 years
    Uh. These are not correct answers -- correct one is "go measure it". Seriously. I have measured performances, and there is big overlap -- slow json parser is slower than fast xml parser and vice versa. Note: XML PARSING is a very low-level thing that can be done fast (ditto for XML). DOM has nothing to do with parsing; it is a tree structure one can optionally build out of low-level tokens. Same for XML Schema which is fully optional. Using such add-ons has significant performance implications (overhead). Same is true for JSON add ons, if true data binding to actual objects is needed.
  • mike clagg
    mike clagg almost 12 years
    most benchmarks put it at 500 times faster in serializations at only a fifth of the cost in transport, results may vary - use as directed.
  • rustyx
    rustyx over 11 years
    @mike - care to share a link with actual results?