How does JSON compare to XML in terms of file size and serialisation/deserialisation time?

前端 未结 8 1790
萌比男神i
萌比男神i 2020-12-28 16:43

I have an application that performs a little slow over the internet due to bandwidth reasons. I have enabled GZip which has improved download time by a significant amout, bu

相关标签:
8条回答
  • 2020-12-28 16:48

    actually this is harder to answer then it seems,

    several years ago json was 'faster' but the differences have become finer between the two.

    what I've observed is;

    • xml compresses better with gzip then json .. time saved in download can offset other components
    • xml parsing/querying in raw js is roughly equiv with json
    • xml parsing/querying in jquery is much slower ... I won't begrudge jquery developers to focus on json

    overall, tech that speeds up modern browsers also applies to xml processing as well.

    generally, whenever I hear json touted as a 'lowfat' alternative to XML I wonder if its not some subliminal obsession with weight issues ... thats on my pessimistic days;

    basically the rule of thumb I follow is

    markup good for documents json good for data

    and move on ... nothing to see here

    0 讨论(0)
  • 2020-12-28 16:49

    Generally speaking, JSON is much faster and smaller than the equivalent XML. XML is richer in that you can store metadata (attributes) and content separately, but the same can be achieved in JSON with appropriate structuring conventions.

    0 讨论(0)
  • 2020-12-28 16:52
    <person firstname='Fred' 
            lastname='Flintstone' 
            age='38' 
            spouse='Wilma'/>
    
    "person":{"firstname":"Fred", 
              "lastname":"Flintstone",  
              "age":38, 
              "spouse":"Wilma" }
    

    XML: 80 chars

    JSON: 92 chars.

    XML is the 13% slimmer winner, who would have thought for all the bogus claims?

    [Example taken from Cheeso above. Note that carriage return types change the size but equally for both. Without any returns in either, the ratio is XML: 74, JSON: 86, a 14% difference]

    Someone claimed in this discussion that "using attributes for content is cheating" (citing [this][1]).

    1) Pray tell how well formed XML is 'cheating'? If it doesn't cheat the (very long) spec, then let's please get over it. Valid is valid.

    2) In that case an ASP.NET web.config is 'cheating', among a thousand other big-dog examples.

    <forms loginUrl="~/Home/Login" 
           defaultUrl="~/Home" 
           timeout="2880" 
           requireSSL="false" 
           slidingExpiration="true" />
    

    To be fair, there are those who tend to have a bloated-XML mentality in how they form the XML.

    But it should not be said that the 13% slimmer XML variety is not just as valid. And thus it even beats JSON (in size).

    [1]: http://www.ibm.com/developerworks/xml/library/x-eleatt/index.html [2004 article, mind you]

    0 讨论(0)
  • 2020-12-28 17:02

    Yes JSON would be about 30% faster there are fewer characters going over the line and its very quick to parse.

    Ypu could also take a look at "YAML" which sends an absolute mininmum of metadata in the message.

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

    The best way to answer this is to test it yourself, since compression is involved. You also neatly avoid the XML vs JSON holy war by having an objective answer!

    Since it's just a test and doesn't really need to work, you could just write up an xml->json converter in javascript that walked the XML DOM tree and copied it into a nested array/object structure then passed it to JSON.stringify(). The only tricky bit would be deciding what becomes an array and what becomes an object.

    Find a representative sample (or a few) of the data being sent, convert it to JSON with your tool, gzip it and the original XML and compare sizes.

    Note: I looked around for an online XML->JSON converter and they were all terrible--copius whitespace outside of quotes (illegal in JSON, alters size) and unquoted key names (ditto). Don't use them for your test or you'll get bad data.

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

    There are different ways of representing JSON such as BSON and CBOR which reduce the size of it.

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