Can browser systems compress keywords in large arrays of re-created objects?

In the spirit of these two questions:

How does the browser handle large arrays of the same object types, are their key names compressed in memory? I once used a graphics library and got a performance boost by shortening the object key names and so I kind of stuck with this set of minds. It seems that if I were to use an array of 1,000,000 such objects, not :

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

      

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

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

      

Although it would seem that the former should use about twice as much memory due to its long key names?

+3


source to share


1 answer


There are two different things you can talk about. The first and simpler are JSON strings, that is, the data that you receive, for example, from a web service. It's a string and everything in the string matters, it's the JSON property names or even spaces. It's a good idea to minimize this in order to reduce network load (you can also gzip the data to get a nice effect).

The actual size of the string probably won't be an issue since you usually don't hold a JSON string for a long time. This is because when you parse JSON, you get a standard JavaScript object.

So what you are most likely asking is if a JavaScript object with longer property names has more memory than those with shorter property names. The answer to that is obviously yes, since you need to put information somewhere. But this is only half the answer; it gets more interesting when you look at multiple objects with the same set of properties.



This is where string interning comes into play. Good JavaScript engines will use string interning for strings, so after one string contains "firstNameAsWrittenInID"

, every other string containing the same value should be able to reuse that interned string object, resulting in a small amount of memory. So in this case, there is no difference between reusing a long or a short string.

Of course, the original string needs to be saved once, so if you have many long properties that don't repeat, it's wise to shorten them. But if you reuse property names all the time, it probably won't cause additional memory overhead.

+2


source







All Articles