Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is parsing JSON faster than parsing XML

I'm creating a sophisticated JavaScript library for working with my company's server side framework.

The server side framework encodes its data to a simple XML format. There's no fancy namespacing or anything like that.

Ideally I'd like to parse all of the data in the browser as JSON. However, if I do this I need to rewrite some of the server side code to also spit out JSON. This is a pain because we have public APIs that I can't easily change.

What I'm really concerned about here is performance in the browser of parsing JSON versus XML. Is there really a big difference to be concerned about? Or should I exclusively go for JSON? Does anyone have any experience or benchmarks in the performance difference between the two?

I realize that most modern web developers would probably opt for JSON and I can see why. However, I really am just interested in performance. If there's a proven massive difference then I'm prepared to spend the extra effort in generating JSON server side for the client.

like image 259
geme_hendrix Avatar asked Jan 04 '11 17:01

geme_hendrix


People also ask

Is it faster to parse XML or JSON?

Parsing speed varies with the technique used. Pure JavaScript parsing generally performs better with XML than with JSON, while query speed generally is faster for JSON. Both with exceptions though where the contrary is true. Using the JavaScript library jQuery imposes a steep penalty on JSON and even worse on XML.

Is JSON easier to parse?

The array data must be a valid JSON string. This is an uncommon use for JSON. parse() . It's easier to declare an array in code instead of going through JSON first.

Is JSON parsing slow?

parse: 702 ms. Clearly JSON. parse is the slowest of them, and by some margin.

Why XML is preferred over JSON?

Usually JSON is more compact, and faster to parse. Prefer XML if: You need to process the data on the client, and you can leverage XSL for that. Chances are the XML + XSL chain will work faster than JSON + JavaScript especially for big chunks of data.


1 Answers

JSON should be faster since it's JS Object Notation, which means it can be recognized natively by JavaScript. In PHP on the GET side of things, I will often do something like this:

<script type="text/javascript">     var data = <?php json_encode($data)?>; </script> 

For more information on this, see here:

Why is Everyone Choosing JSON Over XML for jQuery?

Also...what "extra effort" do you really have to put into "generating" JSON? Surely you can't be saying that you'll be manually building the JSON string? Almost every modern server-side language has libraries that convert native variables into JSON strings. For example, PHP's core json_encode function converts an associative array like this:

$data = array('test'=>'val', 'foo'=>'bar'); 

into

{"test": "val", "foo": "bar"} 

Which is simply a JavaScript object (since there are no associative arrays (strictly speaking) in JS).

like image 81
treeface Avatar answered Sep 21 '22 00:09

treeface