最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What is the maximum length of a string in JavaScript? - Stack Overflow

programmeradmin0浏览0评论

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 ment "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.

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 ment "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.

Share Improve this question edited May 27, 2019 at 3:11 hashed_name 5517 silver badges22 bronze badges asked May 27, 2019 at 3:05 temporary_user_nametemporary_user_name 37.2k48 gold badges160 silver badges243 bronze badges 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) – Vaughan Hilts 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. – Vaughan Hilts 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. – Shidersz 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. – Randy Casburn Commented May 27, 2019 at 4:03
Add a ment  | 

1 Answer 1

Reset to default 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/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length

发布评论

评论列表(0)

  1. 暂无评论