开发者

Node.js get MAX-VALUE of new Set()

How do I get the maximum possible size of a new Set() in Node.js Javascript?

let mySet = new Set();
mySet.MA开发者_如何学JAVAX_VALUE;

Only .size is possible as a get size.


Set instances are bounded only by the available memory (or an arbitrary limit imposed by the environment). There's no specified way to determine that limit (if, in fact, it can even be described in terms of a maximum number of elements).

The size property's getter returns a number, so in theory if the Set had more than Number.MAX_SAFE_INTEGER elements in it, that number could be imprecise. That doesn't necessarily imply that the Set can't have that many elements (but that would be a truly massively large Set). The specification for size has the true count of elements converted to the number type at the point of access.


In a comment you've said:

I want to know how large mySet can be on my machine.

That may depend on what you're putting in the set, but as far as I know, you can only determine that by building a set until adding to it fails.

On my machine using Node v18, I can add 16,777,216 BigInt instances to a set before it fails. And I can add exactly that same number of unique objects to it where each object contains a copy of that BigInt (that is, a set of larger things), suggesting that Node.js limits the size of the Set to that many elements regardless of memory (since neither of those pushes my memory boundaries).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜