Is parsing JSON faster than parsing XML

前端 未结 11 736
盖世英雄少女心
盖世英雄少女心 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: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:

    
      
         
            -73.948753
            40.712784
         
         
            -23.948753
            20.712784
         
      
     
         
            -13.948753
            10.712784
         
      
    
    

    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.

提交回复
热议问题