Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the maximum length of a string in JavaScript?

I thought this would be an easy google search but nothing came up.

Say I create a loop that continually appends one character at a time to a variable.

At what point will the program crash?

I expect it will take <6 seconds for someone to comment "Why don't you try it and find out?", but trying it wouldn't tell me the answer to the question. Is it implementation-specific? Is it defined anywhere? It doesn't seem to be in the ECMAScript spec.

Are there any known limitations beyond available memory? Does performance degrade with strings holding Mbs or Gbs of text?

For the record, I did try running that loop....and it just crashed my browser :( Maybe if I make some adjustments and try again.

like image 308
temporary_user_name Avatar asked Oct 15 '22 14:10

temporary_user_name


1 Answers

ECMAScript 2016 (ed. 7) established a maximum length of 2^53 - 1 elements. Previously, no maximum length was specified. In Firefox, strings have a maximum length of 2**30 - 2 (~1GB). In versions prior to Firefox 65, the maximum length was 2**28- 1 (~256MB).

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length

like image 164
NVRM Avatar answered Oct 20 '22 05:10

NVRM