Javascript 类型的Javascript最大大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4695187/
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-08-23 13:39:49  来源:igfitidea点击:

Javascript maximum size for types?

javascriptcross-browserecma262

提问by Incognito

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.

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

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

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

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

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

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.

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

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

...所以也许这是需要在浏览器中实现的东西?

ECMA does however tell us about numbers, for example,

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

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.

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

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?

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

采纳答案by Ivo Wetzel

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

正如你已经说过的,规范并没有说明除了用于任何类型的大小限制/要求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).

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

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

这限制了 2^27-1 的 32 位版本中最大可能的分配请求。最大扁平字符串长度为 ~2^28(512MB 空间),最大字符串长度为 2^29-1,因此这些限制都不能解决问题(如果它们确实存在,我们将抛出 Out-Of-Memory 异常) )。

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

请参阅: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.

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

回答by Jens Fiederer

ECMA section 6.1.4 is explicit about this.

ECMA 第 6.1.4 节对此进行了明确说明。

"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"

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

回答by hashchange

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

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