Maximum Line and String Lengths

H

Hal Vaughan

Is there a maximum length for Javascript program lines?

What about strings? Is there a limit on string length?

I found some references that said the maximum string length was 256
characters, but I have a program that created a string of over 25,000
characters (the browser was Konqueror).

Are there limits on these lengths? If I require a newer browser for the
program I'm writing, would that change the situation?

Thanks!

Hal
 
F

Fabian

Hal Vaughan hu kiteb:
Is there a maximum length for Javascript program lines?

The maximum limit on line length is whatever you feel comfortable
reading and editing. Personally, any code of mine that won't fit on the
screen without wrapping will be rewritten.

There probably is some limit somewhere, but there isn't any good reason
to push against it that I can see.
What about strings? Is there a limit on string length?

I found some references that said the maximum string length was 256
characters, but I have a program that created a string of over 25,000
characters (the browser was Konqueror).

Are there limits on these lengths? If I require a newer browser for
the program I'm writing, would that change the situation?

I have yet to come across a situation where the maximum string length
became an issue before teh speed at which it could be processed became
an issue. One of the array nests in a scipt of mine is 20k in length. I
consider that neer the upper limit of acceptable processing speed given
teh amount of images it wangs in the process of reading the array.


--
 
H

Hal Vaughan

Thanks!
Hal Vaughan hu kiteb:


The maximum limit on line length is whatever you feel comfortable
reading and editing. Personally, any code of mine that won't fit on the
screen without wrapping will be rewritten.

There probably is some limit somewhere, but there isn't any good reason
to push against it that I can see.

For general HTML and JavaScript I don't like pushing it, either. I read one
page (while I was searching for a maximum line length) that said in pre
Netscape 3.0 days he saw many pages written in 1 line to save on transfer
speeds with slower dialups. While interesting, that doesn't seep to apply
any longer.
I have yet to come across a situation where the maximum string length
became an issue before teh speed at which it could be processed became
an issue. One of the array nests in a scipt of mine is 20k in length. I
consider that neer the upper limit of acceptable processing speed given
teh amount of images it wangs in the process of reading the array.

I'm doing something similar. I am working with up to 3 dimensional arrrays.
In a few cases, I have to store long lists of true/false settings. I'm
storing the arrays by using loops to convert them to a string and storing
that in a hidden field in the form. This also makes it easy for me to move
the data back and forth from Perl to JavaScript programs. However, that
results in the long string I mentioned. The odd thing is that it loads in
quickly and is quickly converted from a one line string to an array, but it
seems to take longer to store the array in one long string.

Thanks for the help!

Hal
 
R

Richard Cornford

... . The odd thing is that it loads in quickly and is
quickly converted from a one line string to an array, but it
seems to take longer to store the array in one long string.

Look at the Array.prototype.join method. E.g.:-

var ar = [true,false,true,false,true];

alert('var ar = ['+ ar.join(',') +'];');

As it is a native code method it is faster than looping through an
array.

It doesn't necessarily help with nested arrays but there are ways of
working round that by assigning custom toString function that call -
this.join() - on the array to which they are attached. E.g.:-

var ar2D = [true,
false,
["string1","string2"],
true,
false,
true
];

ar2D[2].toString = function(){
return '[\"'+this.join('\",\"')+'\"]';
};

alert('var ar2D = ['+ar2D.join(',')+'];');

Richard.
 
H

Hal Vaughan

Richard said:
... . The odd thing is that it loads in quickly and is
quickly converted from a one line string to an array, but it
seems to take longer to store the array in one long string.

Look at the Array.prototype.join method. E.g.:-

var ar = [true,false,true,false,true];

alert('var ar = ['+ ar.join(',') +'];');

As it is a native code method it is faster than looping through an
array.

It doesn't necessarily help with nested arrays but there are ways of
working round that by assigning custom toString function that call -
this.join() - on the array to which they are attached. E.g.:-

var ar2D = [true,
false,
["string1","string2"],
true,
false,
true
];

ar2D[2].toString = function(){
return '[\"'+this.join('\",\"')+'\"]';
};

alert('var ar2D = ['+ar2D.join(',')+'];');

Richard.

I didn't know about these methods (I'm self taught, so I miss a lot in every
language -- it's frustrating). I'll look into them. They may help, but in
most cases I'm working with nested arrays. While your example of creating
a 2d array would work, I'm not sure how to be sure, when converting an
array to a string, that a particular element is an array instead of a
string. It seems I'd still have to do a lot of looping.

I'll have to investigate this, since it could save me a lot of time. The
current code seems to work fast enough, but it would make development on
the rest of the program faster and easier (and more efficient code) if I
could get the process you've shown to work on a multi-dimensional array.

Thanks!

Hal
 
R

Randy Webb

Hal said:
Richard Cornford wrote:

I didn't know about these methods (I'm self taught, so I miss a lot in every
language -- it's frustrating). I'll look into them. They may help, but in
most cases I'm working with nested arrays. While your example of creating
a 2d array would work, I'm not sure how to be sure, when converting an
array to a string, that a particular element is an array instead of a
string. It seems I'd still have to do a lot of looping.

To find out if its an array or a string, use the typeof operator

alert(typeof(myVar))

<URL:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsoprtypeof.asp
/>
 
R

Richard Cornford

I'll have to investigate this, since it could save me a lot
of time. The current code seems to work fast enough, but it
would make development on the rest of the program faster and
easier (and more efficient code) if I could get the process
you've shown to work on a multi-dimensional array.

When - join - is called it type-converts each element in the Array to a
string so when one of those elements is an Array the type-converting is
done by calling its toString method. If that Array contains another then
it too will be type-converted by having its toString method called, so
the effect is recursive to any depth of nesting. That is fairly
reliable.

How practical it would be to use a combination of Array.prototype.join
and custom toString functions to serialise nested arrays into strings is
going to depend quite a lot on the nature of those arrays. For example,
If none of the arrays ever contained string values the whole process
could be relatively simple (assuming there was no need to call the
toString method of an Array elsewhere) as:-

Array.prototype.toString = function(){
return '['+this.join(',')+']';
}

-would result in that custom toString method being inherited by all of
the Arrays used. Indeed strings are only a problem if you want to have
them delimited with quote marks (or similar), if that isn't important
then the above should work anyway.

Richard.
 
H

Hal Vaughan

Richard said:
I'll have to investigate this, since it could save me a lot
of time. The current code seems to work fast enough, but it
would make development on the rest of the program faster and
easier (and more efficient code) if I could get the process
you've shown to work on a multi-dimensional array.

When - join - is called it type-converts each element in the Array to a
string so when one of those elements is an Array the type-converting is
done by calling its toString method. If that Array contains another then
it too will be type-converted by having its toString method called, so
the effect is recursive to any depth of nesting. That is fairly
reliable.

How practical it would be to use a combination of Array.prototype.join
and custom toString functions to serialise nested arrays into strings is
going to depend quite a lot on the nature of those arrays. For example,
If none of the arrays ever contained string values the whole process
could be relatively simple (assuming there was no need to call the
toString method of an Array elsewhere) as:-

Array.prototype.toString = function(){
return '['+this.join(',')+']';
}

-would result in that custom toString method being inherited by all of
the Arrays used. Indeed strings are only a problem if you want to have
them delimited with quote marks (or similar), if that isn't important
then the above should work anyway.

Richard.

This is definitely something I'll play around with so I can see it work and
incorporate it into my code.

Thanks, again!

Hal
 

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
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top