Connecting Tech Pros Worldwide Help | Site Map

Maxmimum length of a line?

Rich MacDonald
Guest
 
Posts: n/a
#1: Jul 23 '05
Short question: Is there any limit to the length of a single line in
javascript?

(Not in the FAQ; came up short in google, and haven't found the really good
references yet.)

Long question: I'm writing a n-depth hierarchical select function and I
need to seed the options data. I could do something like:

1)
data = new Array(1, 2, 3);
data_0 = new Array(11, 12, 13);
data_0_0 = new Array(111, 112, 113);
etc

Or 2)
data = new Array(1, new Array(11, new Array(111, 112, 113), 12, 13), 2, 3);

This is going to be auto-generated stuff, so the unreadability of the 2nd
approach doesn't bother me. So my prime concern is which is the
better/faster approach. I'd benchmark the two, but I first want to make
sure the 2nd approach is ok. If the options are lengthy, then the line in
the 2nd approach could be very long.

I'm sure this is old hat for many/most. I'm an experienced smalltalk/java
programmer but I'm still getting on my feet in javascript. If you have
comments on the alternatives, I'd love to hear them. For example, how do
most javascript implementations implement object attributes and arrays.
With HashMaps and Arrays? IOW, what are performance underpinnings of
javascript that I should know and understand? And can anyone provide refs
to this knowhow? TIA.

P.S. An answer such as: "javascript is fast enough on the client that this
is a silly issue to worry about" is a good one, if its true.
Randy Webb
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Maxmimum length of a line?


Rich MacDonald wrote:
[color=blue]
> Short question: Is there any limit to the length of a single line in
> javascript?[/color]

Hmmmm, good question.
[color=blue]
> (Not in the FAQ; came up short in google, and haven't found the really good
> references yet.)
>
> Long question: I'm writing a n-depth hierarchical select function and I
> need to seed the options data. I could do something like:
>
> 1)
> data = new Array(1, 2, 3);
> data_0 = new Array(11, 12, 13);
> data_0_0 = new Array(111, 112, 113);
> etc[/color]

use an Object instead.

var data = new Object();
data.1 = new Object(11,12,13);
data.1.11 = new Object(111,112,113);

And so on. There are better constructs for doing that, such as the one
below, but it hinders readability when debugging.
[color=blue]
> Or 2)
> data = new Array(1, new Array(11, new Array(111, 112, 113), 12, 13), 2, 3);
>
> This is going to be auto-generated stuff, so the unreadability of the 2nd
> approach doesn't bother me. So my prime concern is which is the
> better/faster approach. I'd benchmark the two, but I first want to make
> sure the 2nd approach is ok. If the options are lengthy, then the line in
> the 2nd approach could be very long.[/color]

Do it the first way. Even if no other reason than readability to debug
any errors until its error free. It also remedies 99% of the problem
with line/variable lengths.
[color=blue]
> P.S. An answer such as: "javascript is fast enough on the client that this
> is a silly issue to worry about" is a good one, if its true.[/color]

Javascript speed is directly dependent on the code though :)


--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Lee
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Maxmimum length of a line?


Rich MacDonald said:[color=blue]
>
>Short question: Is there any limit to the length of a single line in
>javascript?
>
>(Not in the FAQ; came up short in google, and haven't found the really good
>references yet.)
>
>Long question: I'm writing a n-depth hierarchical select function and I
>need to seed the options data. I could do something like:
>
>1)
>data = new Array(1, 2, 3);
>data_0 = new Array(11, 12, 13);
>data_0_0 = new Array(111, 112, 113);
>etc
>
>Or 2)
>data = new Array(1, new Array(11, new Array(111, 112, 113), 12, 13), 2, 3);
>
>This is going to be auto-generated stuff, so the unreadability of the 2nd
>approach doesn't bother me. So my prime concern is which is the
>better/faster approach. I'd benchmark the two, but I first want to make
>sure the 2nd approach is ok. If the options are lengthy, then the line in
>the 2nd approach could be very long.[/color]

The simplest, and probably most efficient approach is:

data = [ 1, [ 11, [ 111, 112, 113 ] ],
2, [ 21, 22, 23, [ 231, 232 ] ]
];

You shouldn't have any problem with line length, but if you want it to be
readable at all, insert line breaks.

VK
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Maxmimum length of a line?


# 2 is better

ECMA specs do not impose any limits on literal length. Maximum array
length is 2^32. Two points to pay attention though:

1) ECMA specs is often kind of Pirats' code: "not a rule, but rather a
guideline" for browser producers (you know to who the biggest stone
flies).

2) Your script will work not on some abstract Babbidge machine, but on
a real computer with some limits on memory and stack depth (specially
it's true for thin clients like PDA and phones). So where and when your
n-sized array will reach these limits - it can be answered only during
experiments.

Closed Thread