Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do browser engines compress keynames in large arrays of reoccurring objects?

Tags:

javascript

In the spirit of these two questions:

  • Is it worth the effort to try to reduce JSON size?
  • JSON response objects: "pretty" keys and larger response or short keys and smaller response?

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?

like image 271
Rafael Emshoff Avatar asked May 26 '15 13:05

Rafael Emshoff


People also ask

What is the role of a rendering engine in a browser?

Before diving into the role of a rendering engine in browsers, let’s quickly understand the underlying architecture of a web browser. A web browser is a software application that enables a user to access and display web pages or other online content through its graphical user interface.

What are the different types of browser engines?

The list below mentions browser engines used by a few common browsers: 1 Google Chrome and Opera v.15+: Blink 2 Internet Explorer: Trident 3 Mozilla Firefox: Gecko 4 Chrome for iOS and Safari: WebKit

How many records should I push to a browser?

The maximum number of usable records I would push to a browser would be around 25,000 records (3.87MB). Keep in mind there are numerous factors to keep in mind when determining how many records you should return to your JavaScript application.

How to give your users a seamless experience with web browsers?

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators By Jash Unadkat, Technical Content Writer at BrowserStack - November 15, 2019 Before diving into the role of a rendering engine in browsers, let’s quickly understand the underlying architecture of a web browser.


1 Answers

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.

like image 137
poke Avatar answered Oct 01 '22 13:10

poke