javascript 变量有存储限制吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30194088/
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
Do javascript variables have a storage limit?
提问by Ajay Gangisetti
Do javascript variables have a storage capacity limit?
javascript 变量有存储容量限制吗?
I'm designing one YUI datatable where I fetch the data from database and store it in a js object and wherever required I'll extract it and update the YUI datatable. Right now in Dev I've very few records and its storing correctly. In production I may have 1000s of records, this js object is capable to store all these 1000s of records?
我正在设计一个 YUI 数据表,我从数据库中获取数据并将其存储在一个 js 对象中,并且在需要的地方我将提取它并更新 YUI 数据表。现在在开发中,我的记录很少,而且存储正确。在生产中我可能有 1000 条记录,这个 js 对象能够存储所有这些 1000 条记录吗?
If its not capable I'll create on hidden textarea in jsp and store the data there
如果它不能,我将在 jsp 中的隐藏文本区域上创建并将数据存储在那里
回答by ssube
Yes, objects and arrays have storage limits. They are sufficiently large to be, for most purposes, theoretical. You will be more limited by the VM than the language.
是的,对象和数组有存储限制。它们足够大,在大多数情况下都是理论上的。与语言相比,您将受到 VM 的更多限制。
In your specific case (sending thousands of items to a client), you will run into the same problem whether it is JSON, JavaScript, or plain text on the JSP page: client memory. The client is far more likely to run out of usable system memory than you are to run into a language restriction. For thousands of small objects, this shouldn't be an issue.
在您的特定情况下(向客户端发送数千个项目),无论是 JSON、JavaScript 还是 JSP 页面上的纯文本,您都会遇到相同的问题:客户端内存。与遇到语言限制相比,客户端更有可能耗尽可用的系统内存。对于数以千计的小物体,这应该不是问题。
Arrays have a limit of 4.2 billion items, shown in the spec at 15.4.2.2, for example. This is caused by the length being a 32-bit counter. Assuming each element is a single integer, that allows you to store 16GB of numeric data in a single array.
例如,数组的限制为 42 亿项,如规范中的 15.4.2.2所示。这是由长度为 32 位计数器引起的。假设每个元素都是一个整数,这允许您在单个数组中存储 16GB 的数字数据。
The semantics on objects are more complex, but most functions to work with objects end up using arrays, so you're limited to 4.2 billion keys in most practical scenarios. Again, that's over 16GB of data, not counting the overhead to keep references.
对象的语义更复杂,但大多数处理对象的函数最终都使用数组,因此在大多数实际场景中,您仅限于 42 亿个键。同样,这超过 16GB 的数据,不计算保留引用的开销。
The VM, and probably the garbage collector, will start to hang for long periods of time well before you get near the limits of the language. Some implementations will have smaller limits, especially older ones or interpreters. Since the JS spec does not specify minimumlimits in most cases, those may be implementation-defined and could be much lower (this questionon the maximum number of arguments discusses that).
在您接近语言的极限之前,VM,可能还有垃圾收集器,将开始长时间挂起。一些实现会有较小的限制,尤其是较旧的或解释器。由于 JS 规范在大多数情况下没有指定最小限制,这些可能是实现定义的并且可能低得多(这个关于最大参数数量的问题讨论了这一点)。
With a good optimizing VM that tries to track the structures you use, at that size, will cause enough overhead that the VM will probably fall back to using maps for your objects (it's theoretically possible to define a struct representing that much data, but not terribly practical). Maps have a small amount of overhead and lookup times get longer as size increases, so you will see performance implications: just not at any reasonable object size.
使用一个试图跟踪您使用的结构的良好优化 VM,在该大小下,将导致足够的开销,VM 可能会回退到为您的对象使用映射(理论上可以定义一个表示那么多数据的结构,但不能非常实用)。Maps 有少量的开销,并且随着大小的增加查找时间会变长,所以你会看到性能影响:只是不在任何合理的对象大小上。
If you run into another limit, I suspect it will be 65k elements (2^16), as discussed in this answer. Finding an implementation that supports less than 65k elements seems unlikely, as most browsers were written after 32 bit architectures became the norm.
如果您遇到另一个限制,我怀疑它将是 65k 个元素 (2^16),如本答案所述。找到支持少于 65k 元素的实现似乎不太可能,因为大多数浏览器都是在 32 位架构成为常态之后编写的。
回答by Razvan Dumitru
There isn't such a limit.
没有这样的限制。
It looks that there is a limit at 16GB, but you can read some tests below or in @ssube's answer.
看起来有 16GB 的限制,但您可以阅读下面或@ssube 的答案中的一些测试。
But probably when your object/json is around 50 mb you'll encounter strange behaviour.
但可能当您的对象/json 大约为 50 mb 时,您会遇到奇怪的行为。
For Json here is an interesting article : http://josh.zeigler.us/technology/web-development/how-big-is-too-big-for-json/
对于 Json,这是一篇有趣的文章:http: //josh.zeigler.us/technology/web-development/how-big-is-too-big-for-json/
For Js Object you have more knowledge here: javascript object max size limit(saying that there isn't such a limit but encounter strange behaviour at ~40 mb)
对于 Js 对象,您在这里有更多知识:javascript object max size limit(说没有这样的限制,但在 ~40 mb 时遇到奇怪的行为)
回答by chrisbergr
The limit depends on the available memory of the browser. So every PC, Mac, Mobile setup will give you a different limit. I don't know how much memory one of your records needs, but I would guess that 1000 records should work on the most machines.
该限制取决于浏览器的可用内存。因此,每台 PC、Mac、移动设置都会给您不同的限制。我不知道你的一条记录需要多少内存,但我猜 1000 条记录应该可以在大多数机器上工作。
But: You should avoid storing massive data amounts in simple variables, depending on the records memory it slows down the whole website behavior. Your users with average computers may see ugly scrolling effects, delayed hover effects and so on..
但是:您应该避免在简单变量中存储大量数据,这取决于记录内存会减慢整个网站的行为。您使用普通计算机的用户可能会看到丑陋的滚动效果、延迟的悬停效果等等。
I would recommend you to use local storage. I'm sorry to don't know the YUI library, but I am pretty sure that you can point to the storage for your datatable source.
我建议您使用本地存储。我很抱歉不知道 YUI 库,但我很确定您可以指向数据表源的存储。
回答by Jacob Finamore
There is a limit on JavaScript objects max js object limit. What i would suggest using is session objects because that's what it sounds like your trying to do anyway.
JavaScript 对象max js object limit有一个限制。我建议使用的是会话对象,因为这听起来像是您尝试做的事情。