Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
188
rated 0 times [  192] [ 4]  / answers: 1 / hits: 47577  / 14 Years ago, tue, january 4, 2011, 12:00:00

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.


More From » xml

 Answers
34

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


[#94380] Monday, January 3, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
collinisaaka

Total Points: 194
Total Questions: 105
Total Answers: 104

Location: Tonga
Member since Tue, Nov 30, 2021
3 Years ago
;