7

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

  1. Is it limited by javascript, or by browser? (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?

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?

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)

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.

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.

Adam Baranyai
  • 3,635
  • 3
  • 29
  • 68
  • 1
    possible duplicate of [javascript object max size limit](http://stackoverflow.com/questions/5926263/javascript-object-max-size-limit) – sergdenisov May 16 '15 at 13:47
  • Couldn't find an answer to my question in that post:| – Adam Baranyai May 16 '15 at 13:51
  • 2
    I'll let others answer the question you actually asked, but I'd like to point out that this is probably a senseless approach. You're going to lot of work to re-create something the browser already does for you, and you're not going to do as good a job as the browser already does. If the browser cache isn't doing what you want it to, read up on HTTP caching rules and headers and make sure you're doing the right things to ensure your data is cacheable. – hobbs May 16 '15 at 14:04

1 Answers1

1

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

ToUint32: (Unsigned 32 Bit Integer)

The abstract operation ToUint32 converts its argument to one of 2^32 integer values in the range 0 through 2^32−1, inclusive.

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

uraimo
  • 19,081
  • 8
  • 48
  • 55
  • The memory available for allocation is controled by what? Can I figure out somehow if I reach that limit, BEFORE trying to add more data to the object? – Adam Baranyai May 16 '15 at 13:56
  • Nice question. Controlled by two things: the browser, that could have a memory limit *per script* or your os that could have a limit for the browser process. I don't think you can know if/how much memory it's available, i'd say that information is not accessible inside the javascript sandbox. Same thing for knowing if an allocation lead to an out of memory issue, you can't know it. – uraimo May 16 '15 at 14:01
  • 1
    A simple way to test what happens could be to grow a string variable in a while loop concatenating some stuff each time and see what happens, not much and not much you can respond to IMO. – uraimo May 16 '15 at 14:02
  • I already tried that, several times, and it always ended in the same thing: my browser crashing:P So you are implying that there is no real way to know how much data I can store in an object, before it would eventually start lagging the browser, or crash it? – Adam Baranyai May 16 '15 at 14:04
  • Yes, i recommend to keep that cache size under control, better be safe than causing a crash :) – uraimo May 16 '15 at 14:08
  • I am now using a cache size of maximum 2MB, and it is working for now:) Wonder how could we test what happens on a much larger variable... – Adam Baranyai May 17 '15 at 11:03