简体   繁体   English

我如何知道在 JavaScript 中构造 Set 的大小限制是多少?

[英]How do I know what is the size limit for constructing a Set in JavaScript?

I am making a tool for deleting geometry from a model in browser.我正在制作一个用于从浏览器中的模型中删除几何图形的工具。

At some step of the deletion process, I take an array full of indexes and create a Set from it to get the array of unrepeated indexes.在删除过程的某个步骤,我获取一个充满索引的数组并从中创建一个 Set 以获取未重复索引的数组。

function GetSetFromArray(array){
    return new Set(array);
}

This function has worked good for many models at the beginning.这个功能一开始对很多型号都很好用。 Since I want to process the deletion as fast as possible, I wanted to avoid using solutions that require .indexOf.因为我想尽快处理删除,所以我想避免使用需要 .indexOf 的解决方案。

But when I try to delete from a big model, for example, an array with a length of roughly 15,000,000.但是当我尝试从一个大模型中删除时,例如,一个长度约为 15,000,000 的数组。 I get the following error on the browser's console:我在浏览器的控制台上收到以下错误:

Uncaught (in promise) RangeError: Set maximum size exceeded Uncaught (in promise) RangeError: Set maximum size exceeded

控制台错误截图

I want to know, what is the maximum size for creating a Set?我想知道,创建 Set 的最大尺寸是多少? Is it something that depends on the Browser?它取决于浏览器吗? Or on the PC running the Browser?还是在运行浏览器的 PC 上? Is there a way to get that number?有没有办法得到那个号码? I think that maybe if I could get the size limit of the Set, I could then split the array to make a series of Sets and then use that?我想也许如果我能得到集合的大小限制,我就可以分割数组来制作一系列集合然后使用它?

The collection containing the items in a Set is called a "List" in the specification, and a limit on the size of a List isnot specified .包含 Set 中项目的集合在规范中称为“List”,并且没有指定List 的大小限制。

These sequences may be of any length这些序列可以是任意长度

Nor is the size of Set lists in particular specified.也没有特别指定 Set 列表的大小。 ( 1 , 2 ) ( 1 , 2 )

  1. Set set.[[SetData]] to a new empty List.将 set.[[SetData]] 设置为一个新的空列表。

So, it must be implementation-defined.因此,它必须是实现定义的。

Is it something that depends on the Browser?它取决于浏览器吗? Or on the PC running the Browser?还是在运行浏览器的 PC 上?

It will depend on the JavaScript engine the browser uses (for example, V8, or SpiderMonkey, or JavaScriptCore, among others).这将取决于浏览器使用的 JavaScript 引擎(例如,V8、SpiderMonkey 或 JavaScriptCore 等)。

Is there a way to get that number?有没有办法得到那个号码?

Just .add items to a Set until it throws.只需.add项目添加到 Set 直到它抛出。

 const getSetSizeLimit = () => { let i = 0; const set = new Set(); while (true) { try { set.add(i); } catch(e) { return i; } i++; } return i; }; console.log(getSetSizeLimit());

On my Windows-Chrome, the limit looks to be 16777216, which is 2 ** 24 .在我的 Windows-Chrome 上,限制看起来是 16777216,即2 ** 24

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM