JavaScript 对象中的属性数量是否有限制?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9282869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:10:28  来源:igfitidea点击:

Are there limits to the number of properties in a JavaScript object?

javascriptobjectproperties

提问by David Laberge

We have an object with more than 75000 properties. The format of the object is as following:

我们有一个拥有超过 75000 个属性的对象。对象的格式如下:

// The key starts with 3 letters and then is followed by 8 numbers
var bigArray = {'AAA########':123456789,
                'AAA########':123456790,
                'AAA########':123456791
               }; 

Is there a known limit of the quantity of properties for JavaScript objects?From my tests the object still works at 65500 elements.

JavaScript 对象的属性数量是否有已知限制?从我的测试来看,该对象仍然可以在 65500 个元素下工作。

  • In Windows 7, IE9 the script crashes (error -2147024882).
  • Windows XP, IE8 works fine.
  • 在 Windows 7、IE9 中,脚本崩溃(错误 -2147024882)。
  • Windows XP,IE8 工作正常。

采纳答案by David Laberge

From our test on this issue it seems that IE9, Windows 7, limits the number of line in a HTA script to 65535. I did not find any source on the issue, it is just the results of our tests.

从我们对这个问题的测试来看,IE9、Windows 7似乎将HTA脚本中的行数限制为65535。我没有找到有关该问题的任何来源,这只是我们测试的结果。

回答by David Laberge

In the current version of Chrome (Sept 2017), I'm limited to around 8.3 million keys. Try pasting this in your browser console:

在当前版本的 Chrome(2017 年 9 月)中,我被限制在大约830 万个键。尝试将其粘贴到浏览器控制台中:

let obj = {};
let keyCount = 0;
while(1) {
  obj[Math.random()] = Math.random();
  if(++keyCount % 10000 === 0) console.log(keyCount);
}

I get an identical limit in Node.js:

我在 Node.js 中得到了相同的限制:

node --max-old-space-size=20000 -e "let obj = {}; let keyCount = 0; while(1) { obj[Math.random()] = Math.random(); if(++keyCount % 10000 === 0) console.log(keyCount); }"

Interestingly, if I use a Map, I can get about 16.8 millionkeys before it crashes (you can get past this limit with something like this).

有趣的是,如果我使用Map,我可以在它崩溃之前获得大约1680 万个键(你可以通过类似这样的东西超过这个限制)。

回答by Nico

I'm not sure what the actualvalue is, but I see the practicalupper limit around 400,000 in node.js (on a Mac with 16 GB of RAM).

我不确定实际值是多少,但我看到node.js 中的实际上限约为 400,000(在具有 16 GB RAM 的 Mac 上)。

Here is a log of me adding rows from a database into an object:

这是我将数据库中的行添加到对象中的日志:

[[21:32:34.325]] [LOG] 340001, pint of delight
[[21:32:35.574]] [LOG] 350001, pound shrimp
[[21:32:36.545]] [LOG] 360001, ravioli allaragosta
[[21:32:37.721]] [LOG] 370001, roasted ham and cheese
[[21:32:39.862]] [LOG] 380001, salmon kama
[[21:32:41.152]] [LOG] 390001, scallops and vegetables
[[21:32:42.150]] [LOG] 400001, show cabernet ca
[[21:32:44.412]] [LOG] 410001, sloppy nachos
[[21:33:25.425]] [LOG] 420001, spaghetti or ziti sauce
[[21:35:37.839]] [LOG] 430001, steak au poivre vert
[[21:37:37.202]] [LOG] 440001, sushi moriawase
[[21:39:45.365]] [LOG] 450001, tequila shooters
[[21:42:09.036]] [LOG] 460001, toro roll with scallion
[[21:44:32.796]] [LOG] 470001, two enchiladas taco rice and refried beans
[[21:47:02.584]] [LOG] 480001, veuve clicquot ponsardin rose reims nv
[[21:49:04.020]] [LOG] 490001, whole turkey gourmet sides
[[21:51:15.264]] [LOG] finished

Until around 400,000 it takes about 1 second to insert 10,000 new records. Past 410,000, the time increases almost exponentially.

在大约 400,000 之前,插入 10,000 条新记录大约需要 1 秒。超过 410,000,时间几乎呈指数增长。

I'm not sure how I'll solve this. Maybe make 2 objects and limit them to 400,000 keys each... a bit labor-intensive but better than rewriting a dictionary object :)

我不确定我将如何解决这个问题。也许制作 2 个对象并将它们限制为每个 400,000 个键......有点劳动密集型但比重写字典对象要好:)

Update:It looks like it's actually the amount of memoryused that is the issue and not so much the number of objects. My machine slows to a crawl at about 1.5 GB of RAM used. It might be linked to the memory allocated to the node.js process, which can be increased with this parameter: --max_old_space_size=4096 (number is in MB).

更新:看起来实际上是使用的内存量是问题,而不是对象的数量。我的机器在使用大约 1.5 GB 的 RAM 时会变慢。它可能链接到分配给 node.js 进程的内存,可以通过以下参数增加:--max_old_space_size=4096(数字以 MB 为单位)。

回答by talnicolas

The exact maximum limit of an array is 2^32 - 1 or 4294967295, due to restrictions in Javascript's memory.

由于 Javascript 内存的限制,数组的确切最大限制是 2^32 - 1 或 4294967295。

Link

关联

回答by crush

It's going to be 2^32 - 1; however, specific browsers may limit it further.

它将是 2^32 - 1;但是,特定的浏览器可能会进一步限制它。