9

In the spirit of these two questions:

How does a browser handle large arrays of the same object-types, are their keynames somehow compressed in memory? I once used a graphics library and got a performance gain by shortening the key names of the objects, and so I am kind of stuck to that mind set. It seems though that it would not make a difference if I used an array of 1,000,000 such objects:

[{
    "firstNameAsWrittenInID": Pete,
    "lastNameAsWrittenInID": Jenkins
},{
    "firstNameAsWrittenInID": Jane,
    "lastNameAsWrittenInID": Jenkins
},
...
{
    "firstNameAsWrittenInID": Johann,
    "lastNameAsWrittenInID": Abele
}]

or an array of 1,000,000 of such objects:

[{
    "f": Pete,
    "l": Jenkins
},{
    "f": Jane,
    "l": Jenkins
},
...
{
    "f": Johann,
    "l": Abele
}]

Although it would seem that the first should use about twice the memory due to it's long key-names?

Community
  • 1
  • 1
Rafael Emshoff
  • 2,541
  • 1
  • 35
  • 47
  • What do you mean by "JSON objects"? Javascript Objects aquired by parsing a JSON string, or the JSON string itself? – doldt May 26 '15 at 13:03
  • 1
    JSON *strings* are just strings, so shortening the property names (and removing whitespace, etc.) will reduce the size of the string. When you parse the string, a JavaScript object is created however; so you are probably interested whether objects with long property names take more memory than those with shorter names, and whether reused property names will have any effect on this. – poke May 26 '15 at 13:05
  • 1
    You seem to use the terms JSON, array and object interchangeably; I've cleaned up the post a bit but it's still unclear if you *really* mean "arrays of objects" when the example code doesn't have arrays at all. – JJJ May 26 '15 at 13:07
  • How often do these long key names appear in an input string? If a few long key names appears frequently then it gzips well during transport, and string pooling in JSON.parse can mitigate the memory footprint of the parsed result. – Mike Samuel May 26 '15 at 13:26

1 Answers1

7

There are two different things you can talk about here. The first, and simpler one is JSON strings, i.e. the data you for example receive from a web service. This is a string, and everything in a string matters, being that JSON property names, or even whitespace. It’s a good idea to minimize this in a way to reduce the network load (you can also gzip the data to get a good effect).

The actual string size is likely not going to be an issue since you usually don’t keep the JSON string around for long. That is because when you parse JSON, you get a standard JavaScript object back.

So what you are more likely asking for is whether JavaScript objects with longer property names are taking more memory than those with shorter property names. The answer to that is obviously yes, since you need to put the information somewhere. But that is only half the answer; it’s getting more interesting when you look at multiple objects with the same set of properties.

Here, string interning comes into play. Good JavaScript engines will use string interning for strings, so after having one string contain "firstNameAsWrittenInID", every other string containing the same value should be able to reuse that interned string object resulting in a small memory footprint. So in this case, there is no difference between reusing a long or a short string.

Of course, the original string needs to be stored once, so if you have many long properties that are not repeated, it’s a good idea to shorten them in a way. But if you reuse property names all the time, it’s likely that this will not cause any additional memory overhead.

poke
  • 369,085
  • 72
  • 557
  • 602
  • Thanks, I learned something new : ) Also of related interest: [Flyweight pattern](http://en.wikipedia.org/wiki/Flyweight_pattern), [JavaScript and string interning](http://stackoverflow.com/questions/5276915/do-common-javascript-implementations-use-string-interning). – Rafael Emshoff May 27 '15 at 07:24