Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do javascript variables have a storage limit?

Do javascript variables have a storage capacity limit?

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?

If its not capable I'll create on hidden textarea in jsp and store the data there

like image 365
Ajay Gangisetti Avatar asked May 12 '15 14:05

Ajay Gangisetti


People also ask

How much data can a variable store in JavaScript?

JavaScript variables can hold numbers like 100 and text values like "John Doe". In programming, text values are called text strings. JavaScript can handle many types of data, but for now, just think of numbers and strings. Strings are written inside double or single quotes.

How much data can a variable store?

A variable can only store one value at a time – a number or a string. A value stored in a variable can be used once or many times in a program.

Are JavaScript variables stored in memory?

“Variables in JavaScript (and most other programming languages) are stored in two places: stack and heap. A stack is usually a continuous region of memory allocating local context for each executing function.

How large can a JavaScript object be?

Chrome can support no more than 2GB of object in memory. it does slow down and eventually freeze once the total memory usage reaches around 2GB.


2 Answers

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.

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.

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.

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.

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 minimum limits in most cases, those may be implementation-defined and could be much lower (this question on the maximum number of arguments discusses that).

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.

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.

like image 51
ssube Avatar answered Oct 11 '22 11:10

ssube


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.

But probably when your object/json is around 50 mb you'll encounter strange behaviour.

For Json here is an interesting article : 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)

like image 25
Razvan Dumitru Avatar answered Oct 11 '22 11:10

Razvan Dumitru