string length question

I

ipellew

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
 
R

Richard Cornford

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.
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.
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.
 
L

Lasse Reichstein Nielsen

Whats the maximum string length in Javascript.

The specification does not define a maximal length ...
Is it the same across all the browsers?

.... so it's unlikely that all implementations have the same limits.
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
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 10 Jul 2006
14:38:09 remote, seen in Lasse Reichstein
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.
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.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top