Connecting Tech Pros Worldwide Forums | Help | Site Map

string length question

ipellew@pipemedia.co.uk
Guest
 
Posts: n/a
#1: Jul 10 '06
Hi;

Whats the maximum string length in Javascript.

Is it the same across all the browsers?

max_s_len = str.length // what the max number of chars we can have is
str?

Regards
Ian


Richard Cornford
Guest
 
Posts: n/a
#2: Jul 10 '06

re: string length question


ipellew@pipemedia.co.uk wrote:
Quote:
Whats the maximum string length in Javascript.
Are you asking about the value of a string primitive or the maximum
length of a string literal in source code? I have never seen stated
values for any maximum length of a string primitive's value (so
probably limited by available memory), and have seen practical tests
going into the hundreds of millions of characters (so probably
exceeding all practical needs). Microsoft used to claim a size limit
for a string literal token, but previous discussions on the subject
proved that the stated figure no longer applied and that the current
real figure is so large that nobody wanted to waste the time necessary
to find out what the limit was.
Quote:
Is it the same across all the browsers?
There is no reason to expect any limit to be the same across browsers
except by coincidence (such as would result from using a 32bit integer
value to hold the length in different implementations). If in practice
the limiting factor tends to be available memory then all browsers on
the same machine will have the same available memory.
Quote:
max_s_len = str.length // what the max number of chars we can
have is str?
If you really must know you can carry out your own tests. You will find
that as the strings get bigger the code slows down so much that you
will get fed up with waiting before you find a definitive answer.

Richard.

Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#3: Jul 10 '06

re: string length question


ipellew@pipemedia.co.uk writes:
Quote:
Whats the maximum string length in Javascript.
The specification does not define a maximal length ...
Quote:
Is it the same across all the browsers?
.... so it's unlikely that all implementations have the same limits.
Quote:
max_s_len = str.length // what the max number of chars we can have is
str?
If we insist that all lengths must be expressible exactly as a number,
then the limit will be 2^53. At one byte per character (which is
probably too low), that requires ~8 petabyte of memory. So this
theoretical limit is unlikely to be the limiting factor.

A more likely limit is the range of an UInt32.

A quick script to check the limit (although probably not in reasonable
time):

var N = 32; // vary this and see if it works.
var x = "x";
for(var i = 0; i < N; i++) { x= x+x; };
alert(x.length)

Try this in different browser, starting with a lower N and see when
it breaks :)

In practice, my guess is that long strings take much memory and time
to manipulate, so you'll want to make them shorter long before you
reach a hard limit.
/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Dr John Stockton
Guest
 
Posts: n/a
#4: Jul 10 '06

re: string length question


JRS: In article <d5cd7hzi.fsf@hotpop.com>, dated Mon, 10 Jul 2006
14:38:09 remote, seen in news:comp.lang.javascript, Lasse Reichstein
Nielsen <lrn@hotpop.composted :
Quote:
>ipellew@pipemedia.co.uk writes:
>
Quote:
>Whats the maximum string length in Javascript.
Quote:
>If we insist that all lengths must be expressible exactly as a number,
>then the limit will be 2^53. At one byte per character (which is
ECMA requires 16-bit units, but not that they are characters.
Quote:
>probably too low), that requires ~8 petabyte of memory. So this
>theoretical limit is unlikely to be the limiting factor.
>
>A more likely limit is the range of an UInt32.
Some routines use -1 or lower, which could change that to SInt32.

Some implementations seem likely to use DWORD internally, imposing a
limit at 2^32-1 bytes.
Quote:
>A quick script to check the limit (although probably not in reasonable
>time):
>
>var N = 32; // vary this and see if it works.
>var x = "x";
>for(var i = 0; i < N; i++) { x= x+x; };
>alert(x.length)
>
>Try this in different browser, starting with a lower N and see when
>it breaks :)
Or put the alert inside the loop, changing it to a Confirm, and continue
the loop until the computer or the user gives out.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Closed Thread