27

Can JavaScript string store 100K characters? I've written a script where a string from PHP is passed to a variable in JavaScript. It works fine when it is cut short to almost ten thousand characters but breaks the script when attempting to pass the entire string whose length is a bit greater than 100K. No errors could be found though. Is there any solution for this as to any way of increasing character limit of JavaScript variable? I'm just a beginner. Would appreciate is some one could find a solution for this.

4
  • What do you mean "breaks the script"?
    – Xymostech
    Dec 4, 2012 at 6:51
  • 2
    See stackoverflow.com/questions/5926263/… for some relevant info. My guess is the limit is in PHP, not in JS.
    – jfriend00
    Dec 4, 2012 at 6:52
  • 5
    Open the JS console and try this for yourself. str = ""; for(var i = 0; i < 100000; i++) { str = str + "x"; } then type str and hit enter. See what happens. Dec 4, 2012 at 6:57
  • 2
    my browser crashes.. Jan 19, 2016 at 15:21

3 Answers 3

38

The ECMAScript Standard ECMA-262 (6th Edition, June 2015) says

6.1.4 The String Type

The String type is the set of all ordered sequences of zero or more 16-bit unsigned integer values ("elements") up to a maximum length of 253-1 elements.

So don't plan on using more than 9,007,199,254,740,991 or about 9 quadrillion characters. Of course, you should be prepared for systems which cannot allocate 18 PB chunks of memory, as this is not required for conforming ECMAScript implementations.

1
  • I like this answer a lot -- I wrote it, after all -- but please remember to vote for Jack's answer if you vote for this.
    – Charles
    Apr 24 at 14:27
24

I think the question is asking about the practical limit, not the spec limit. And, no, it is not always the amount of RAM you have. I have x86_64 24GB PC running Linux Mint with x86_64 Firefox and x86_64 Chrome, and the limits I ran into were:

  • 1,073,741,822 limit in Firefox 84
  • 536,870,888 limit in Chrome 87

Any higher and Firefox throws a Uncaught RangeError: repeat count must be less than infinity and not overflow maximum string size, whereas Chrome throws Uncaught RangeError: Invalid string length. Use the following snippet to run a binary search for the max string length in your browser:

for (var startPow2 = 1; startPow2 < 9007199254740992; startPow2 *= 2)
    try {" ".repeat(startPow2);} catch(e) {
        break;
    }

var floor = Math.floor, mask = floor(startPow2 / 2);
while (startPow2 = floor(startPow2 / 2))
    try {
        " ".repeat(mask + startPow2);
        mask += startPow2; // the previous statement succeeded
    } catch(e) {}

console.log("The max string length for this browser is " + mask);

1
  • 2
    I think this is a fantastic answer and I think it’s a crime that my answer (solid as it may be) has 25x as many votes. Vote for this, people!
    – Charles
    Jul 12, 2021 at 20:05
7

There is no theorical limit to JS or PHP on the size of their strings.

I think there are a few possible situations.

Firstly, check that you are not sending your string via HTTP GET. There is a maximum size for GET and i think its dependent on your web server.

Secondly, if you do use POST, check in php.ini for post_max_size and see if it is smaller than the string size you are sending to it as well as your .htacccess file to see if php_value post_max_size is not too small.

Thirdly, check that in php.ini that your memory_limit does not restrict the size of memory that your script can use.

hope this helps.

7
  • Thanks for your reply. I've written the javascript in the same php page and I'm assigning the php variable to a javascript variable on the same page. If I echo the variable I can see the output but once assigned to js nothing gets executed below that.
    – Rohith
    Dec 4, 2012 at 23:12
  • just a shot, but did you regenerate any single quotes or double quotes that can cause your js to break. did you escape them before passing to js? Is there any error in the javascript console?
    – Aaron Lee
    Dec 5, 2012 at 8:03
  • There is no error in javascript console. No single and double quotes are regenerated as well.
    – Rohith
    Dec 6, 2012 at 0:35
  • I've even tried assigning the value from php directly to js as in I echoed the output from php and copied it to the script instead of passing it dynamically. Still it did not work. It is strange that it doesn't show any errors.
    – Rohith
    Dec 6, 2012 at 1:57
  • Sorry, I'm not supposed to as per my company norms. Anyways I can give you a better idea. I'm fetching data from mysql database and storing it as string. It concatenates and becomes very huge depending on the amount of data fetched. This string is passed to javascript variable to generate a graph using jqplot. Everything works well unless the string is around 100 thousand characters long. String of length of almost 50,000 works fine. I thought the jqplot function would be breaking but nothing under the line where the variable is passed gets executed.
    – Rohith
    Dec 6, 2012 at 3:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.