Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript object max size limit

I'm trying to pass a JavaScript variable to the server-side using jquery.ajax method.

I'm trying to create a json string, but when the length of variable reaches 10000, no more data is appended to the string.

var jsonObj = '{"code":"' + code + '","defaultfile":"' + defaultfile + '","filename":"' + currentFile + '","lstResDef":[';         $.each(keys, function(i, item) {             i = i + 1;             var value = $("#value" + i).val();             var value = value.replace(/"/g, "\\\"");             jsonObj = jsonObj + '{';             jsonObj = jsonObj + '"Key":' + '"' + Encoder.htmlEncode($(this).html()) + '"' + "," + '"Value"' + ':' + '"' + Encoder.htmlEncode(value) + '"';             jsonObj = jsonObj + '},';             alert(jsonObj);                      });                    jsonObj = jsonObj + ']}'; 

Here, when the character length of the var jsonObj is 10000, the values following that is not appended.

It looks like there is some limit about that.

like image 643
Paras Avatar asked May 08 '11 07:05

Paras


People also ask

What is the size of object in JS?

Answer: Use the Object. keys() Method keys() method along with the length property to get the length of a JavaScript object. The Object. keys() method returns an array of a given object's own enumerable property names, and the length property returns the number of elements in that array.

What is maximum size of variable in JavaScript?

JavaScript imposes a limit of 254 characters for each string variable assignment in your program.

How many properties can a JS object have?

Summary. JavaScript objects have two types of properties: data properties and accessor properties.


2 Answers

There is no such limit on the string length. To be certain, I just tested to create a string containing 60 megabyte.

The problem is likely that you are sending the data in a GET request, so it's sent in the URL. Different browsers have different limits for the URL, where IE has the lowest limist of about 2 kB. To be safe, you should never send more data than about a kilobyte in a GET request.

To send that much data, you have to send it in a POST request instead. The browser has no hard limit on the size of a post, but the server has a limit on how large a request can be. IIS for example has a default limit of 4 MB, but it's possible to adjust the limit if you would ever need to send more data than that.

Also, you shouldn't use += to concatenate long strings. For each iteration there is more and more data to move, so it gets slower and slower the more items you have. Put the strings in an array and concatenate all the items at once:

var items = $.map(keys, function(item, i) {   var value = $("#value" + (i+1)).val().replace(/"/g, "\\\"");   return     '{"Key":' + '"' + Encoder.htmlEncode($(this).html()) + '"' + ",'+     '" + '"Value"' + ':' + '"' + Encoder.htmlEncode(value) + '"}'; }); var jsonObj =   '{"code":"' + code + '",'+   '"defaultfile":"' + defaultfile + '",'+   '"filename":"' + currentFile + '",'+   '"lstResDef":[' + items.join(',') + ']}'; 
like image 173
Guffa Avatar answered Sep 19 '22 15:09

Guffa


Step 1 is always to first determine where the problem lies. Your title and most of your question seem to suggest that you're running into quite a low length limit on the length of a string in JavaScript / on browsers, an improbably low limit. You're not. Consider:

var str;  document.getElementById('theButton').onclick = function() {   var build, counter;    if (!str) {     str = "0123456789";     build = [];     for (counter = 0; counter < 900; ++counter) {       build.push(str);     }     str = build.join("");   }   else {     str += str;   }   display("str.length = " + str.length); }; 

Live copy

Repeatedly clicking the relevant button keeps making the string longer. With Chrome, Firefox, Opera, Safari, and IE, I've had no trouble with strings more than a million characters long:

 str.length = 9000 str.length = 18000 str.length = 36000 str.length = 72000 str.length = 144000 str.length = 288000 str.length = 576000 str.length = 1152000 str.length = 2304000 str.length = 4608000 str.length = 9216000 str.length = 18432000

...and I'm quite sure I could got a lot higher than that.

So it's nothing to do with a length limit in JavaScript. You haven't show your code for sending the data to the server, but most likely you're using GET which means you're running into the length limit of a GET request, because GET parameters are put in the query string. Details here.

You need to switch to using POST instead. In a POST request, the data is in the body of the request rather than in the URL, and can be very, very large indeed.

like image 40
T.J. Crowder Avatar answered Sep 21 '22 15:09

T.J. Crowder