PHP: is JSON or XML parser faster?

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.


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!)

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

The comment from Adam above convinced me to benchmark it. Using[format], I found that simplexml_load_string() is SLIGHTLY faster than json_decode(). But the difference is practically a margin of error.

Test #1 time (xml): 3.75221395493 seconds
Test #2 time (xml): 4.1562371254 seconds
Test #3 time (xml): 3.60420489311 seconds
Test #4 time (xml): 3.85622000694 seconds
Test #5 time (xml): 3.89622211456 seconds


Test #1 time (json): 4.53225803375 seconds
Test #2 time (json): 4.06823205948 seconds
Test #3 time (json): 4.03222990036 seconds
Test #4 time (json): 3.80421590805 seconds
Test #5 time (json): 3.88022208214 seconds

on the following code (where I’ve already curl’ed the data to a file, data.[xml,json]).


$test = 'json';  //xml or json
$data = implode(file("data.".$test),"\r\n");

for ($t=1; $t<=5; $t++) {
    $start[$t] = microtime(1);
    for ($i=0; $i<3000; $i++) {
        if ($test == 'xml') $xml = simplexml_load_string($data);
        else $json = json_decode($data);
    $end[$t] = microtime(1);
    echo "<p>Test #{$t} time ({$test}): " . ($end[$t] - $start[$t]). " seconds</p>";

Solution 2

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 3

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 4

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.

Solution 5

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 6

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.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply