Caching in javascript

Is there a limit on the amount of data I can store inside a javascript variable? If yes:

  • Is it limited to javascript or browser? (is it a fixed number or a variable number?)

  • What to do if the limit is reached or exceeded? Browser crashing, or javascript giving error message?

If I make many ajax calls, on different pages, and I want to save the result of these ajax calls in a global variable in javascript for future use (to free up the number of server requests and speed up the response that the user will receive), is it guaranteed that my data will be stored in this variable?

For example:

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

Is there a limit on the amount of data I can store in the cache object? If so, can I somehow check if the data that will be stored is stored, and if not, free the cache? (e.g. using try / catch)

EDIT: a possible duplicate does not answer my question, because I want to know the javascript object limit, not the string, and also does not answer what happens when the limit is reached.

There should be a limit, but it would be nice to know if this limit comes from javascript or the browser, and if I can somehow check if this limit is reached to solve the problem accordingly.

+7
javascript
source share
1 answer

The only hard limit I can think of to look at your sample is the size of the array, which is defined in the ECMAScript standard as the maximum value that can be represented in an unsigned 32-bit integer (via ToUint32) :

ToUint32: (Unsigned 32-bit integer)

The abstract operation ToUint32 converts its argument to one of 2 ^ 32 integer values ​​in the range from 0 to 2 ^ 32-1 inclusive.

No other restrictions exist in the most common variable, except for the memory available for allocation, if you have enough memory in which the variable will be stored, if not, then it will not (I think that it will not be gracefully distorted).

There is no way to find out that something went wrong during distribution, the best approach is to decide in advance how much memory your cache will use to the maximum and stick to this maximum size (limiting the size of the array or using a circular array, considering that it is a cache).

+1
source share

All Articles