JavaScript 类型的最大大小?

发布于 2024-10-12 06:04:43 字数 764 浏览 5 评论 0原文

通过研究 javascript 类型,我试图找出某些数据类型的最大存储大小是多少。例如,我设置了一个快速递归算法来增加 var 大小,直到浏览器崩溃,最终在我现有的 chrome 版本上,字符串大小接近 128mb(或者可能是 256)。

我一直在以痛苦的方式这样做,因为我找不到任何关于此的规范,但不断的浏览器崩溃使这成为一次痛苦的尝试(由于某种原因,对于这个特定问题,try catch 似乎毫无用处)。

我正在寻找有关其他类型的最大存储大小的信息(数组、对象、函数、字符串、数字、布尔...)

EMCA-262 第 8.4 节对此含糊其辞

字符串的长度是其中元素(即 16 位值)的数量。空字符串的长度为零,因此不包含任何元素。

...所以也许这需要被识别为在浏览器中实现?

然而,ECMA 确实告诉我们有关数字的信息,例如,

Number 类型正好有 18437736874454810627 个(即 2^64−2^53+3)个值,表示 IEEE 二进制浮点运算标准中指定的双精度 64 位格式 IEEE 754 值,除了 IEEE 标准的 9007199254740990(即 2^53−2)个不同的“非数字”值在 ECMAScript 中表示为单个特殊 NaN 值之外。

但后来我看不到任何关于物体的东西。

我可以期待在浏览器中使用什么?是否有任何代码库可以帮助管理非常大的对象和字符串?

我可以在单个脚本中分配多少内存?

Looking into javascript types I'm trying to find out what the maximum storage size for some data types are. For instance, I set up a quick recursive algo to increase var size till the browser crashes, which ends up being somewhere close to 128mb (or maybe it's 256) for strings on my existing version of chrome.

I've been doing it the painful way because I couldn't find any specs on this, but constant browser crashes make this a painful trial (try catch seems useless for some reason with this particular issue).

I'm looking for information about maximum storage size for the other types also (array, object, functions, strings, numbers, bools...)

EMCA-262 section 8.4 is vague on this

The length of a String is the number of elements (i.e., 16-bit values) within it. The empty String has length zero and therefore contains no elements.

...so perhaps this is something that needs to be identified as implemented in browsers?

ECMA does however tell us about numbers, for example,

The Number type has exactly 18437736874454810627 (that is, 2^64−2^53+3) values, representing the double-precision 64-bit format IEEE 754 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic, except that the 9007199254740990 (that is, 2^53−2) distinct “Not-a-Number” values of the IEEE Standard are represented in ECMAScript as a single special NaN value.

But then I don't see anything about objects.

What can I expect for use in browsers? Is there any code-base out there that helps manage very large objects and strings?

How much memory can I allocate in a single script?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

回忆凄美了谁 2024-10-19 06:04:43

正如您已经说过的,除了 Number 之外,该规范没有声明任何类型的大小限制/要求。

所以这肯定是留给实施的。

例如,Chrome 对字符串的限制似乎是硬编码在 512mb 左右(32 位上更少)。

这限制了最大
2^27-1 的 32 位版本中可能的分配请求。最大扁弦
长度为~2^28(512MB空间),最大字符串长度为2^29-1,所以两者都不是
这些限制捕获了问题(我们会抛出内存不足异常,而不是如果
他们做到了)。

请参阅:http://code.google.com/p/v8/issues/detail? id=362#c9

就其他浏览器而言,这需要一些研究,例如查看 Firefox 的代码。但我怀疑我们能否为 IE / Opera 做同样的事情。

As you already stated, the specification does not state any size limits / requirements for types besides Number.

So this is definitally left to the implementation.

For example, Chrome's limit on strings seems to be hard coded at around 512mb (and less on 32bit).

This puts a limit on the maximally
possible allocation request in 32-bit versions of 2^27-1. The maximal flat string
length is ~2^28 (512MB space), and the maximal string length is 2^29-1, so neither of
these limits catch the problem (we would throw an Out-Of-Memory exception instead if
they did).

See: http://code.google.com/p/v8/issues/detail?id=362#c9

As far as the other browsers go, this would need some research e.g. looking into Firefox's code. But I doubt we can do the same for IE / Opera.

束缚m 2024-10-19 06:04:43

ECMA 第 6.1.4 节对此有明确说明。

“字符串类型是零个或多个 16 位无符号整数值(“元素”)的所有有序序列的集合,最大长度为 2^53-1 个元素”

ECMA section 6.1.4 is explicit about this.

"The String type is the set of all ordered sequences of zero or more 16-bit unsigned integer values (“elements”) up to a maximum length of 2^53-1 elements"

凹づ凸ル 2024-10-19 06:04:43

我目前遇到这种情况,我无法通过 json 发回结果,因为它是基于 html 的。并且数据被截断到特定大小,多余的数据被丢弃。我还不确定返回结果中的字符数,但远小于 2^27...

I am experiencing this currently, I can not send the results back via json as it is html based. And the data is truncated to a specific size, and the excess data is dropped. I am not positive yet on the number of characters in the returned results, but it is far less then 2^27...

梦屿孤独相伴 2024-10-19 06:04:43

我在另一个答案中列出了字符串的一些典型限制。基本上,您处于安全范围内的字符串长度不超过 227 个字符。

I have listed some typical limits for strings in another answer. Basically, you are in safe territory up to a string length of 227 characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文