6

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.

4
  • 1
    How much memory do you have? :) Operations on a string are defined by their implementation. For example, length is O(1) so there is no consequence of it being larger (perhaps a very small upfront allocation cost, but of course, filling that much memory has a cost) Commented May 27, 2019 at 3:06
  • I think it's pretty unfair to dismiss this question. It's intellectually interesting, at least. Would you prefer to close this rather than than the 4040954590 homework questions? Come on. Commented May 27, 2019 at 3:44
  • In response to It doesn't seem to be in the ECMAScript spec. There is a paragraph on the ECMAScript 2015 - 6.1.4 The String Type about this. Commented May 27, 2019 at 3:58
  • @VaughanHilts - you are correct, it is far more interesting than homework questions. Writing code is about living within limits. Contributing here is about that too - I agree there is nothing wrong with this question. Commented May 27, 2019 at 4:03

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.