简体   繁体   English

在javascript中缓存

[英]Caching in javascript

Is there a limit on the ammount of data I can store inside a javascript variable? 我可以在javascript变量中存储的数据量是否有限制? If yes: 如是:

  1. Is it limited by javascript, or by browser? 是受限于JavaScript还是浏览器? (is it a fixed number, or a variable number?) (它是固定数字还是可变数字?)

  2. What if the limit is reached, or exceeded? 如果达到或超过限制怎么办? Does the browser crash, or javascript throws an error? 浏览器崩溃,或javascript引发错误?

If I am making a lot of ajax calls, to different pages, and I want to store the result of these ajax calls in a global variable in javascript for future use(to free up the amount of queries to the server, and quicken the response the user gets), is it guaranteed that my data will be stored in this variable? 如果我正在对不同的页面进行大量的ajax调用,并且我希望将这些ajax调用的结果存储在javascript中的全局变量中以供将来使用(以释放对服务器的查询量,并加快响应速度)用户获得),是否保证我的数据将存储在此变量中?

For example: 例如:

 function afterAjaxResponse(responseText) {
     cache[ajaxIdentifier]=responseText;
 }

Is there a limit on how many data I can store in the "cache" object? 我可以在“缓存”对象中存储多少数据? If yes, can I check somehow if the data to be stored still fits in it, and if not, free up the cache? 如果是,我可以以某种方式检查要存储的数据是否仍然适合它,如果没有,可以释放缓存吗? (for example with a try/catch) (例如使用try / catch)

EDIT: The possible duplicate doesn't answer my question, because I want to know the limit of a javascript object, not a string, and it also doesn't answer to what happens when the limit is reached. 编辑:可能的重复不回答我的问题,因为我想知道javascript对象的限制,而不是字符串,它也没有回答达到限制时发生的情况。

There must be a limit, but it would be nice to know, if that limit comes from javascript or the browser, and if I can check somehow if that limit is reached, to solve the problem accordingly. 必须有一个限制,但很高兴知道,如果该限制来自javascript或浏览器,并且如果我能以某种方式检查是否达到该限制,则相应地解决问题。

The only hard-limit i can think of looking at your sample is the array size, that is defined in the ECMAScript standard as being the maximum value that can be represented in an unsigned 32bit integer (via ToUint32) : 我可以考虑查看您的示例的唯一硬限制是数组大小,它在ECMAScript标准中定义为可以用无符号32位整数表示的最大值(通过ToUint32)

ToUint32: (Unsigned 32 Bit Integer) ToUint32 :(无符号32位整数)

The abstract operation ToUint32 converts its argument to one of 2^32 integer values in the range 0 through 2^32−1, inclusive. 抽象操作ToUint32将其参数转换为0到2 ^ 32-1(包括0和2 ^ 32-1)范围内的2 ^ 32个整数值之一。

No other limits are present on a generic variable itself, other than the memory available for allocation, if you have enough memory that variable will be stored, if not, it will not (i guess it will not fail gracefully). 除了可用于分配的内存之外,通用变量本身没有其他限制,如果你有足够的内存来存储变量,如果没有,它就不会(我想它不会优雅地失败)。

There is no way for you to know if something went wrong during allocation, the best approach is to decide beforehand how much memory at max your cache will use and stick to that maximum size (limiting array size or using a circular array considering that it's a cache). 你无法知道在分配过程中是否出现问题,最好的方法是事先决定你的缓存最大内存量将使用多少并坚持最大尺寸(限制数组大小或使用圆形数组,因为它是一个高速缓存)。

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

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