Is parsing JSON faster than parsing XML

前端 未结 11 666
盖世英雄少女心
盖世英雄少女心 2020-12-01 04:51

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 for

相关标签:
11条回答
  • 2020-12-01 05:23

    best example i have found about these two is :

    http://www.utilities-online.info/xmltojson/#.VVGOlfCYK7M

    that means JSON is more human readable and understandable than XML.

    0 讨论(0)
  • 2020-12-01 05:28

    It also depends on how your JSON is structured. Tree-like structures tend to parse more efficiently than a list of objects. This is where one's fundamental understanding of data structures will be handy. I would not be surprised if you parse a list-like structure in JSON that might look like this:

    {
            {
                "name": "New York",
                "country":"USA",
                "lon": -73.948753,
                "lat": 40.712784
            },
            {
                "name": "Chicago",
                "country":"USA",
                "lon": -23.948753,
                "lat": 20.712784
            },
            {
                "name": "London",
                "country":"UK",
                "lon": -13.948753,
                "lat": 10.712784
            }
    }
    

    and then compare it to a tree like structure in XML that might look like this:

    <cities>
      <country name="USA">
         <city name="New York">
            <long>-73.948753</long>
            <lat>40.712784</lat>
         </city>
         <city name="Chicago">
            <long>-23.948753</long>
            <lat>20.712784</lat>
         </city>
      </country>
     <country name="UK">
         <city name="London">
            <long>-13.948753</long>
            <lat>10.712784</lat>
         </city>
      </country>
    </cities>
    

    The XML structure may yield a faster time than that of JSON since if I loop through the node of UK to find London, I don't have to loop through the rest of the countries to find my city. In the JSON example, I just might if London is near the bottom of the list. But, what we have here is a difference in structure. I would be surprised to find that XML is faster in either case or in a case where the structures are exactly the same.

    Here is an experiment I did using Python - I know the question is looking at this strictly from a JavaScript perspective, but you might find it useful. The results show that JSON is faster than XML. However, the point is: how you structure is going to have an effect on how efficiently you are able to retrieve it.

    0 讨论(0)
  • 2020-12-01 05:31

    Benchmarks have been done. Here's one. The difference in some of the earlier browsers appeared to be an entire order of magnitude (on the order of 10s of milliseconds instead of 100s of ms), but not massive. Part of this is in server response time - XML is bulkier as a data format. Part of it is parsing time - JSON lets you send JavaScript objects, while XML requires parsing a document.

    You could consider adding to your public API a method to return JSON instead of modifying existing functions if it becomes and issue, unless you don't want to expose the JSON.

    See also the SO question When to prefer JSON over XML?

    0 讨论(0)
  • 2020-12-01 05:31

    since JSON is native in and designed FOR Javascript, it's going to out-perform XML parsing all day long. you didn't mention your server-side language, in PHP there is the json_encode/json_decode functionality built into the PHP core...

    0 讨论(0)
  • 2020-12-01 05:36

    Firstly, I'd like to say thanks to everyone who's answered my question. I REALLY appreciate all of your responses.

    In regards to this question, I've conducted some further research by running some benchmarks. The parsing happens in the browser. IE 8 is the only browser that doesn't have a native JSON parser. The XML is the same data as the JSON version.

    Chrome (version 8.0.552.224), JSON: 92ms, XML: 90ms

    Firefox (version 3.6.13), JSON: 65ms, XML: 129ms

    IE (version 8.0.6001.18702), JSON: 172ms, XML: 125ms

    Interestingly, Chrome seems to have almost the same speed. Please note, this is parsing a lot of data. With little snippets of data, this isn't probably such a big deal.

    0 讨论(0)
  • 2020-12-01 05:41

    Another reason to stick with XML is, that if you switch to JSON, you modify the "maintenance contract". XML is more typed than JSON is, in the sense that it works more naturally with typed languages (i.e. NOT javascript).

    If you change to JSON, some future maintainer of the code base might introduce a JSON array at some point which has mixed type content (e.g. [ "Hello", 42, false ]), which will present a problem to any code written in a typed language.

    Yes, you could do that as well in XML but it requires extra effort, while in JSON it can just slip in.

    And while it does not seem like a big deal at first glance, it actually is as it forces the code in the typed language to stick with a JSON tree instead of deserializing to a native type.

    0 讨论(0)
提交回复
热议问题