浏览器引擎是否会压缩大量重复出现的对象中的键名? [英] Do browser engines compress keynames in large arrays of reoccurring objects?

查看:55
本文介绍了浏览器引擎是否会压缩大量重复出现的对象中的键名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本着以下两个问题的精神:

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?

推荐答案

您可以在此处谈论两种不同的情况.第一个也是更简单的一个是 JSON字符串,即您从Web服务接收的数据.这是一个字符串,字符串中的所有内容都很重要,即JSON属性名称,甚至是空格.最好将此方法最小化,以减少网络负载(您也可以gzip压缩数据以获得良好效果).

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).

实际的字符串大小可能不会成为问题,因为您通常不会长时间保持JSON字符串.那是因为当您解析 JSON时,您会获得一个标准的JavaScript对象.

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.

因此,您更可能要问的是,具有较长属性名称的JavaScript对象是否比具有较短属性名称的JavaScript对象占用更多的内存.答案显然是肯定的,因为您需要将信息放在某处.但这仅仅是答案的一半.当您查看具有相同属性集的多个对象时,它会变得越来越有趣.

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.

在这里,字符串实习开始发挥作用.好的JavaScript引擎将对字符串使用字符串插入,因此,在一个字符串包含"firstNameAsWrittenInID" 之后,其他所有包含相同值的字符串都应能够重用该被插入的字符串对象,从而减少内存占用.因此,在这种情况下,重用长或短字符串没有区别.

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.

这篇关于浏览器引擎是否会压缩大量重复出现的对象中的键名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆