Javascript line size limit: for me, 256 MB is the same for all browsers?

I wonder what maximum line length I can get in Javascript, I myself tested it today on my Firefox 43.0.1 running on Windows 7. I managed to build a string 2^28 - 1 long, but when I tried to create a string with another char Firebug showed me the error "allocation overflow", which means that the line should be less than 256 MB.

Is it the same for all browsers, all computers, all operating systems, or depends on it?

I created the following snippet to find out the limit:

 (function() { strings = ["z"]; try { while(true) { strings.push(strings[strings.length - 1] + strings[strings.length - 1]); } } catch(err) { var k = strings.length - 2; while(k >= 0) { try { strings.push(strings[strings.length - 1] + strings[k]); k--; } catch(err) {} } console.log("The maximum string length is " + strings[strings.length - 1].length); } })(); 

If you are using a different browser / OS, I would like to see your results. My result: the maximum string length is 268435455.

PS: I was looking for an answer for an answer, but the very last topic I found was from 2011, so I'm looking for more relevant information.

+7
javascript string memory out-of-memory
source share
3 answers

Characters are stored at 16 bits

When you see that 256*2**20 characters are in a string, this does not mean that 256 megabytes of memory is allocated. JavaScript stores each character in two bytes (like utf16 encoded by the specification).

A word about the ropes

Today's browsers (even IE) store strings in an expanded form, most often using rope binding .

  • Ropes do not need a dedicated memory area
  • May even deduplicate substrings, i.e. s+s does not necessarily use twice memory as s
  • Concatenation is very fast
  • Access to the item is a bit slower

Having studied some runs in IE and Chrome, I would say that they use some lazy evaluation for strings and will try to expand them periodically. After starting the next snippet, none of the browsers used more memory than before. But if I tried to manipulate the saved window.LONGEST_STRING in the console, IE threw the error out of memory, and chrome froze for a short time and consumed a lot of memory (> 2 GB).

ps: On my laptop, IE11 had a maximum row size of 4 GB, Chrome had 512 MB

Browser behavior

IE11

IE11

Chrome47

Chrome47

Faster algorithm for determining maximum row size

 var real_console_log = console.log; console.log = function(x) { real_console_log.apply(console, arguments); var d = document,b=d.body,p=d.createElement('pre'); p.style.margin = "0"; p.appendChild(d.createTextNode(''+x)); b.appendChild(p); window.scrollTo(0, b.scrollHeight); }; function alloc(x) { if (x < 1) return ''; var halfi = Math.floor(x/2); var half = alloc(halfi); return 2*halfi < x ? half + half + 'a' : half + half; } function test(x) { try { return alloc(x); } catch (e) { return null; } } function binsearch(predicateGreaterThan, min, max) { while (max > min) { var mid = Math.floor((max + min) / 2); var val = predicateGreaterThan(mid); if (val) { min = mid + 1; } else { max = mid; } } return max; } var maxStrLen = binsearch(test, 10, Math.pow(2, 52)) - 1; console.log('Max string length is:'); console.log(maxStrLen + ' characters'); console.log(2*maxStrLen + ' bytes'); console.log(2*maxStrLen/1024/1024 + ' megabytes'); console.log(''); console.log('Store longest string'); window.LONGEST_STRING = alloc(maxStrLen); console.log('Try to read first char'); console.log(window.LONGEST_STRING.charAt(0)); console.log('Try to read last char'); console.log(window.LONGEST_STRING.charAt(maxStrLen - 1)); console.log('Try to read length'); console.log(window.LONGEST_STRING.length); 
+10
source share

A bug report for chrome tracker has this comment:

 ... When allocation fails, we create a Failure pointer encoding the amount requested, as well as some tag and type bits. This puts a limit on the maximally possible allocation request in 32-bit versions of 2^27-1. The maximal flat string length is ~2^28 (512MB space), and the maximal string length is 2^29-1... 

Please note that this is from 2009, so I believe that it still has consequences in current versions of V8, as the previous link refers to the NodeJS tool working within toString() .

+1
source share

Internal implementations may use UCS2 or UTF16. As suggested by @hege_hegedus, at least Firefox uses the Rope structure ( https://dxr.mozilla.org/mozilla-central/search?q=%2Btype-ref%3ARopeBuilder ). The codes give me the following results:

CHROME VERSION 39.0.2171.95 Linux OS Version: 3.13.0-43-generic

Firefox 34.0

Chrome exit (from @@ hege_hegedus code): Maximum line length: 268435440 characters 536870880 bytes 511.9999694824219 megabytes Store long line Try to read first char Try to read last char Try to read length 268435440

Firefox output (from OP code): "Maximum line length is 268435455"

Archived at http://gpupowered.org/string_js.txt

+1
source share

All Articles