Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #28428 > unrolled thread

Recursive function to find a node in the DOM

Started by"danca (Daniele Campagna)" <cyberdanny@tiscalinet.it>
First post2015-07-15 09:59 +0200
Last post2015-08-06 10:20 +0200
Articles 10 on this page of 30 — 5 participants

Back to article view | Back to comp.lang.javascript


Contents

  Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-15 09:59 +0200
    Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-15 11:59 +0200
      Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-15 12:03 +0200
      Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-15 13:36 +0200
        Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-19 12:27 +0200
          Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-19 12:34 +0200
          Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-21 12:08 +0200
            Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-25 14:11 +0200
              Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-25 21:22 +0200
                Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-27 16:12 +0200
      Re: Recursive function to find a node in the DOM "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-08-08 04:50 +0200
        Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-08-08 09:33 +0200
          Re: Recursive function to find a node in the DOM "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-08-12 00:48 +0200
            Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-08-12 01:18 +0200
              Re: Recursive function to find a node in the DOM "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-08-13 23:26 +0200
                Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-08-16 08:48 +0200
                  Re: Recursive function to find a node in the DOM "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-08-20 02:11 +0200
    Re: Recursive function to find a node in the DOM Scott Sauyet <scott.sauyet@gmail.com> - 2015-07-18 15:59 -0700
      Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-21 12:30 +0200
        Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-21 13:53 +0200
          Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-21 14:51 +0200
            Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-21 16:05 +0200
              Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-21 20:35 +0200
              Re: Recursive function to find a node in the DOM Scott Sauyet <scott.sauyet@gmail.com> - 2015-07-30 22:10 -0700
                Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-08-06 10:07 +0200
                  Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-08-06 20:15 +0200
                    Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-08-08 01:21 +0200
          Re: Recursive function to find a node in the DOM Scott Sauyet <scott.sauyet@gmail.com> - 2015-07-30 22:09 -0700
    Re: Recursive function to find a node in the DOM Denis McMahon <denismfmcmahon@gmail.com> - 2015-07-29 03:04 +0000
      Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-08-06 10:20 +0200

Page 2 of 2 — ← Prev page 1 [2]


#28460

From"danca (Daniele Campagna)" <cyberdanny@tiscalinet.it>
Date2015-07-21 14:51 +0200
Message-ID<molf6c$2l1$1@dont-email.me>
In reply to#28459
Il 07/21/2015 01:53 PM, Thomas 'PointedEars' Lahn ha scritto:
> danca (Daniele Campagna) wrote:
>
>> Il 07/19/2015 12:59 AM, Scott Sauyet ha scritto:
>>> Thomas 'PointedEars' Lahn wrote:
>>>> There is no "javascript".  [ ... ]
>>>
>>> Keep tilting at that windmill.  The rest of us know better.
>>
>> :-)
>
> So *that* is what you *really* think.  Noted.
>

Mr. Lahn, please don't take early deductions. A smile is just a smile, 
referred to this old quarrel about "Javascript", and in no way 
irrespective of you or your recognized knowledge. And it is referred to 
an old thread, too, in wich Scott was humorous about that [not sure he 
remembers the thread and me anyway]. So I believe to know he is usually 
quite benign and more on the side of humour than on the one of sarcasm.
And so am I, then "absit injuria risis"!


>>
>> "logically correct" is a part of your answer i like very much, but "a
>> recursive solution is clear and elegant" is what really made my day ;-)
>
> You are easily amused.  This "logic" is flawed.
>

With due respect, a recursive algorythm hardly demonstrates flawed 
logic, I think [if it works].

Dan
-- 
"Everybody should pay taxes with a smile"
I tried, but they want money.

[toc] | [prev] | [next] | [standalone]


#28461

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-07-21 16:05 +0200
Message-ID<1516357.lIjpcpmlPf@PointedEars.de>
In reply to#28460
danca (Daniele Campagna) wrote:

> Il 07/21/2015 01:53 PM, Thomas 'PointedEars' Lahn ha scritto:
>> danca (Daniele Campagna) wrote:
>>> Il 07/19/2015 12:59 AM, Scott Sauyet ha scritto:
>>>> Thomas 'PointedEars' Lahn wrote:
>>>>> There is no "javascript".  [ ... ]
>>>> Keep tilting at that windmill.  The rest of us know better.
>>> :-)
>> So *that* is what you *really* think.  Noted.
> 
> Mr. Lahn,

Contrary to real life, in Usenet it is “you can say you to me” *unless* 
offense has been given.

> please don't take early deductions. A smile is just a smile,
> referred to this old quarrel about "Javascript", and in no way
> irrespective of you or your recognized knowledge.

(You mean “disrespectful”, I presume.)  ACK.

> And it is referred to an old thread, too, in wich Scott was humorous about 
> that [not sure he remembers the thread and me anyway]. So I believe to $
> know he is usually quite benign and more on the side of humour than on the 
> one of sarcasm.

Not my experience and impression.  Historically, Scott has always supported 
the irresponsible approach of the blissfully ignorant of “show me where it 
breaks”, and resorted to personal attacks when the flaw in that thinking was 
made apparent.

> And so am I, then "absit injuria risis"!

That would probably be «rīsū» (ablative, for “concerning the smile”; 
cf. «verbum»).

>>> "logically correct" is a part of your answer i like very much, but "a
>>> recursive solution is clear and elegant" is what really made my day ;-)
>> You are easily amused.  This "logic" is flawed.
> 
> With due respect, a recursive algorythm hardly demonstrates flawed
> logic, I think [if it works].

The flawed "logic" is the one that is based on the claim that the recursive 
algorithm would be “logically correct” in this case.  That appears to me to 
be self-evident as the recursive algorithm can fail while the iterative one 
can *never* fail under the same circumstances.  And as for “clear and 
elegant”, beauty lies in the eye of the beholder; it has nothing to do with 
logic.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#28462

From"danca (Daniele Campagna)" <cyberdanny@tiscalinet.it>
Date2015-07-21 20:35 +0200
Message-ID<mom3ak$mcb$1@dont-email.me>
In reply to#28461
Il 07/21/2015 04:05 PM, Thomas 'PointedEars' Lahn ha scritto:
> danca (Daniele Campagna) wrote:
>
...
>> please don't take early deductions. A smile is just a smile,
>> referred to this old quarrel about "Javascript", and in no way
>> irrespective of you or your recognized knowledge.
>
> (You mean “disrespectful”, I presume.)  ACK.
>

Esatto.

[OT]
>
>> And so am I, then "absit injuria risis"!
>
> That would probably be «rīsū» (ablative, for “concerning the smile”;
> cf. «verbum»).
>

I made my best to forget Latin and Old Greek, so I am very rusty about 
it. I mimicked the sentence "absit injuria verbis" and I treated 
"risus" as "verbum" but it is wrong, "verbum" being II declination and 
"risus" IV. Oops. Probably correct is: "absit injuria risibus". :-)
Latin did not use a singular (as we do in modern languages) to mean a 
plurality, IIRC.

>>
>> With due respect, a recursive algorythm hardly demonstrates flawed
>> logic, I think [if it works].
>
> The flawed "logic" is the one that is based on the claim that the recursive
> algorithm would be “logically correct” in this case.  That appears to me to
> be self-evident as the recursive algorithm can fail while the iterative one
> can *never* fail under the same circumstances.  And as for “clear and
> elegant”, beauty lies in the eye of the beholder; it has nothing to do with
> logic.
>

Nevertheless logic has its own beauty, even if not all the people can 
perceive it. Descartes has been contradicted, and his famous sentence is 
maybe wrong, but remains "clear and elegant".
[/OT]

Dan
-- 
"Everybody should pay taxes with a smile"
I tried, but they want money.

[toc] | [prev] | [next] | [standalone]


#28491

FromScott Sauyet <scott.sauyet@gmail.com>
Date2015-07-30 22:10 -0700
Message-ID<4be984b1-7744-4271-acd8-a6a625977504@googlegroups.com>
In reply to#28461
Thomas 'PointedEars' Lahn wrote:
> danca (Daniele Campagna) wrote:
> 
>> And it is referred to an old thread, too, in wich Scott was humorous about 
>> that [not sure he remembers the thread and me anyway]. So I believe to $
>> know he is usually quite benign and more on the side of humour than on the 
>> one of sarcasm.

"usually quite benign"?  How am I going to live that down among my fellow 
cutthroats, brigands and hooligans?  :-)

I'm more than willing to use sarcasm to help get a point across, especially
with the overly pedantic.  But this is meant only as a light-hearted jab
at Thomas Lahn's ridiculous posturing. 

 
> Not my experience and impression.  Historically, Scott has always supported 
> the irresponsible approach of the blissfully ignorant of "show me where it 
> breaks", 

You really do need to learn to read.

I've never promoted such an approach.

But I certainly do prefer demonstrably correct, easily maintainable code 
over something that might work slightly better in an obscure environment 
that I have no interest in supporting. 

Suggesting that code has to work everywhere that it might ever have run or
might one day run -- when you have no way to even demonstrate that it 
actually works anywhere -- is a clear indication of someone who focuses on
entirely the wrong things.  Someone like that is probably a premature
optimizer too.


>           and resorted to personal attacks when the flaw in that thinking was 
> made apparent.

I have been participating in this group since 2009. I have had only 
cordial exchanges with everyone here, with the exception of two 
participants: David Mark and Thomas Lahn. The second thread I 
particpated in had both an interesting and pleasant discussion with and 
some nasty unpleasantries started by Mr. Lahn. That's been the pattern 
time and again. If I disagree for long, unless it's on an easily 
provable matter, he will find a way to insult me. 


He hasn't responded to me for a while.  I'd assumed that after one of
our run-ins he'd kill-filed me.  I can't say I was disappointed.  Or,
failing that, I guessed that he was filtering out content originating
from the execrable Google Groups, and so wasn't seeing many of my posts.
I don't know how I feel about this reversal.

I expect most people here will recognize which one of us is the more 
likely to start with personal attacks, though. Hint: his ears are 
strangely misshapen. 


-----

So while we're on the subject, here's my theory of why Mr. Lahn decided
to continue arguing against recursion here.  I'm afraid it's not very
flattering.

In the intial response to the OP, who was asking about the need to return
the value from the function, Thomas replied,

| Of course you need a break condition to prevent infinite recursion;

This was a mistake. It had nothing to do with the question, which was 
about argument evaluation strategy, and Javascript's use of 
call-by-sharing. 

My response answered the OP and rebutted Mr. Lahn's point on recursion. 
But he couldn't leave it at that because he really does not like to be 
corrected, unless it's about the most specific, verifiable, documented 
details of something. Anything else, he'll choose to argue, and will as 
often as not, make his response insulting, or at minimum, patronizing. 

I wonder if this sounds as likely to others as it does to me.

  -- Scott

[toc] | [prev] | [next] | [standalone]


#28501

From"danca (Daniele Campagna)" <cyberdanny@tiscalinet.it>
Date2015-08-06 10:07 +0200
Message-ID<mpv4ml$blj$1@dont-email.me>
In reply to#28491
Il 07/31/2015 07:10 AM, Scott Sauyet ha scritto:
> Thomas 'PointedEars' Lahn wrote:
>> danca (Daniele Campagna) wrote:
>>
>>> And it is referred to an old thread, too, in wich Scott was humorous about
>>> that [not sure he remembers the thread and me anyway]. So I believe to $
>>> know he is usually quite benign and more on the side of humour than on the
>>> one of sarcasm.
>
> "usually quite benign"?  How am I going to live that down among my fellow
> cutthroats, brigands and hooligans?  :-)
>

Just hope they don't read this NG! :-)

> -----
>
> So while we're on the subject, here's my theory of why Mr. Lahn decided
> to continue arguing against recursion here.  I'm afraid it's not very
> flattering.
>
> In the intial response to the OP, who was asking about the need to return
> the value from the function, Thomas replied,
>
> | Of course you need a break condition to prevent infinite recursion;
>
> This was a mistake. It had nothing to do with the question, which was
> about argument evaluation strategy, and Javascript's use of
> call-by-sharing.

I had the same suspect.

>
> My response answered the OP and rebutted Mr. Lahn's point on recursion.
> But he couldn't leave it at that because he really does not like to be
> corrected, unless it's about the most specific, verifiable, documented
> details of something. Anything else, he'll choose to argue, and will as
> often as not, make his response insulting, or at minimum, patronizing.
>
> I wonder if this sounds as likely to others as it does to me.
>
>    -- Scott
>

I copy/paste the following paragraph, extracted from a mail I sent 
someone, on the subject:

----
But: one last consideration about the risk to crash due to wild increase 
of the call stack.
You seems to think that if a document has a zillion nodes, the recursive 
function will create a zillion of calls and subsequently a stack of a 
zillion elements. It is not the case. What really matter is how deep is 
a nested node from the starting node, because the algorythm calls itself 
only to go to the next child node, but returns (and decreases the call 
stack) when the current branch of the tree ends!!

So I really don't think that a DOM can have a node nested more than, 
say, a few dozen of levels, and this is the maximum amount the stack 
will reach!
----

Dan
-- 
"Everybody should pay taxes with a smile"
I tried, but they want money.

[toc] | [prev] | [next] | [standalone]


#28505

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-08-06 20:15 +0200
Message-ID<1443069.sYRfn84SoE@PointedEars.de>
In reply to#28501
danca (Daniele Campagna) wrote:

> I copy/paste the following paragraph, extracted from a mail I sent
> someone, on the subject:
> 
> ----
> But: one last consideration about the risk to crash due to wild increase
> of the call stack.
> You seems to think that if a document has a zillion nodes, the recursive
> function will create a zillion of calls and subsequently a stack of a
> zillion elements. It is not the case. What really matter is how deep is
> a nested node from the starting node, because the algorythm calls itself
> only to go to the next child node, but returns (and decreases the call
> stack) when the current branch of the tree ends!!
> 
> So I really don't think that a DOM can have a node nested more than,
> say, a few dozen of levels, and this is the maximum amount the stack
> will reach!
> ----

You know very well that I have replied to your e-mail and you have agreed 
that your argument was not sound.  I told you that I did not make the 
assumption you insuated there, at all.  In fact, you told me you had used 
your script on a large document and it failed; if not because of stack 
overflow, because of something else that you could not determine at the 
time – meaning you could not handle the complexity of the approach.  Now, 
two weeks later you are showing off here as if nothing happened.

All this tells me that you are not trustworthy, and that I have wasted my 
time with you after all.

Score adjusted

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#28507

From"danca (Daniele Campagna)" <cyberdanny@tiscalinet.it>
Date2015-08-08 01:21 +0200
Message-ID<mq3el5$dst$1@dont-email.me>
In reply to#28505
Il 08/06/2015 08:15 PM, Thomas 'PointedEars' Lahn ha scritto:
> danca (Daniele Campagna) wrote:
>
>> I copy/paste the following paragraph, extracted from a mail I sent
>> someone, on the subject:
>>
>> ----
>> But: one last consideration about the risk to crash due to wild increase
>> of the call stack.
>> You seems to think that if a document has a zillion nodes, the recursive
>> function will create a zillion of calls and subsequently a stack of a
>> zillion elements. It is not the case. What really matter is how deep is
>> a nested node from the starting node, because the algorythm calls itself
>> only to go to the next child node, but returns (and decreases the call
>> stack) when the current branch of the tree ends!!
>>
>> So I really don't think that a DOM can have a node nested more than,
>> say, a few dozen of levels, and this is the maximum amount the stack
>> will reach!
>> ----
>
> You know very well that I have replied to your e-mail and you have agreed
> that your argument was not sound.  I told you that I did not make the
> assumption you insuated there, at all.  In fact, you told me you had used
> your script on a large document and it failed; if not because of stack
> overflow, because of something else that you could not determine at the
> time – meaning you could not handle the complexity of the approach.  Now,
> two weeks later you are showing off here as if nothing happened.
>

Fairly correct, apart the point in which I "agreed that my argument was 
not sound".

> All this tells me that you are not trustworthy, and that I have wasted my
> time with you after all.

I don't see any problem in my reporting what *I* wrote you. Someone 
other could decide to comment, or not, and that comment could be in 
agreement with yours, or maybe not. I did not report what *you* answered 
me because I don't want feed flames or whatever, and moreover I don't 
think it would be polite. But I don't see any problem in just reporting 
that. Scott expressed some doubt about the memory consumption problem, 
and some appreciation for the recursive approach in programming, not to 
say the sound explanation about the passing of parameters in ECMAscript. 
It's quite obvious he's not a Sunday programmer. I just repeated my 
argument to him, because I don't think that what has been said by you is 
to be considered definitive. I simply don't agree completely with you, 
or, to be more precise, the assumptions you made don't apply to my 
particular circumstances, so I'm wondering if I can safely ignore the 
risks you cite, *in my case*.

Anyway, here is the copy of my answer:

//-------------------------------
But, as per premises, I am not writing a library for general purposes 
[nor I would be able to, at present time].
The last "reality check" I had, a browser was eating some 200 Mb of 
memory, so I maintain that a recursive function exploring the DOM hardly 
can crash for memory consumption reasons.

BTW I tested it on a quite large (for my standards!) page downloaded 
from Internet and modified in order to: 1)add my recursive function, 2) 
give a name to an arbitrary element and 3) put a button to call the 
function.
This is the report (from Mozilla) for the page weight:


	Size 	Uncompressed Size
21 Documents 	32 KB 	212 KB
23 Images 	19 KB 	19 KB
0 Objects 	
	
43 Scripts 	225 KB 	1786 KB
2 Style Sheets 	21 KB 	54 KB
89 Files 	297 KB 	2070 KB

Apart the fact that the script returned "undefined" - I must still 
investigate what's this - no crash happened, and the maximum recursion 
deep I reached is less than 20 levels (debugging with FF).

This is not to say you are wrong and I am right, but probably you are 
referring to very particular circumstances, while I am talking about a 
server-side generated page (invoked by a [modern] browser) of about 
20-120 Kb and a - in this environment - quite safe recursive function 
that in no way can harm the hosting environment.
//----------------------

As one can see, It is hardly understandable your feeling offended.
I simply had no time to further investigate, since for me right now it's 
mainly an academic question. I use that poor script in a strictly 
controlled environment and wanted just to be sure the function didn't 
cause a crash, so I tested it in a BIG document (and had no crashes).

Please note that I decided to spend time in that particular test because 
your comments, so I think this fact demonstrates I take seriously what 
you say and am simply trying to better understand things.

But even if I respect an opinion, I should not be supposed to accept it 
blindly. I have the right to use my brain to make distinctions.

Unfortunately the script failed for some reason, ok, and maybe one day 
or another I will make some further test (but my primary intent was to 
evaluate the risk of *crashes*).


>
> Score adjusted
>

Unfair.

Dan

-- 
"Everybody should pay taxes with a smile"
I tried, but they want money.

[toc] | [prev] | [next] | [standalone]


#28490

FromScott Sauyet <scott.sauyet@gmail.com>
Date2015-07-30 22:09 -0700
Message-ID<73f72c73-d67d-4ea0-8771-e8ce5908e3de@googlegroups.com>
In reply to#28459
Thomas 'PointedEars' Lahn wrote:
> danca (Daniele Campagna) wrote:
>> Il 07/19/2015 12:59 AM, Scott Sauyet ha scritto:
>>> Thomas 'PointedEars' Lahn wrote:

>>> Recursion is logically correct here.
> 
> Nonsense.

Cite evidence.

 
>>> If you're working with modern browsers, you'll have at least a 
>>> 20000-call deep recursion stack..
> 
> Cite evidence.

I'm sure you can STFW as well as I can for published results, but in the 
spirit of "teach a man to fish...", something like the following would 
demonstrate lower bounds for the recursion stack in a browser 
environment: 

    var ctr = 0;

    setTimeout(function() {
      document.getElementById('output').innerHTML = ctr;
    },1);

    function recursiveTest() {
      ctr++;
      recursiveTest();
    }
    recursiveTest();

My tests bear out my statements, as do the results I've found in 
searching the web. The latest browser I've found that does not support a 
depth of at least 1000 is Safari 3.x, which only has 500 (That's from
published reports, not my own analysis, as I do not have an Apple device
for testing.) 

 
> And if you are working with not so modern browsers, and not so shallow 
> documents, which you cannot know because with scripting you do not know the 
> environment your code will be exposed to, your code will *break*.  So 
> actually you do not need to cite evidence; your argument is that of the 
> blissfully ignorant again.

How do you know whether or not I (or the OP) knows what environment(s) 
our code will be exposed to? Are you sure that Daniele isn't developing 
for a closed-shop intranet operating only with evergreen browsers 
running recent versions of SpiderMonkey? Do you know that I'm not 
writing code to run in some limited subset of Microsoft offerings? 

But all that is really beside the point.

All code can break if exposed to a hostile enough environment. Do you 
know that the device on which your code is running has enough free 
memory to run what you ask it to? Do you know it's not overheating?
What happens when someone pulls the plug or removes the battery?

You talk about recursion depth as though it's a hard-and-fast rule that 
one shouldn't use recursion in Javascript.  Nonsense.  That's the moral 
equivalent of pronouncing that you have found the perfect sorting 
algorithm for lists.  It is an absolute in an area that doesn't admit 
such absolutes.  How big is the list?  How much of your memory can
you reserve for additional copies of items?  Is it expensive to generate
sort keys for each item?  Is it already somewhat sorted, and if so, in 
what order?  And, of course, how maintainable is the code?

One should really ask the same sorts of questions about nearly any 
algorithm introduced into a code base. 

Whether a DOM walker should be recursive or not should be subject to 
these same sorts of considerations:  Where will it run? How deeply nested 
might nodes ever be in these environments?  Can it be easily be made 
tail-recursive to take advantage of newer optimizations?  And, of course,
how maintainable is the code?


> [ ... ]

>>> A recursive solution is clear and elegant.  A non-recursive version would
>>> be much less so.  If you make it tail-recursive, when browsers start to
>>> optimize tail-recursion, the little performance penalty you would
>>> currently pay for recursion will be eliminated.
>> 
>> "logically correct" is a part of your answer i like very much, but "a
>> recursive solution is clear and elegant" is what really made my day ;-)
> 
> You are easily amused.  This "logic" is flawed.

There is an important branch of computer science dedicated to proving 
the correctness of programs. While Tony Hoare's system [1] does allow 
one to work with imperative constructs, it is almost always easier to 
prove the correctness of a system without internal mutable state. What I 
suggested was that the logical solution was the one using recursion. 
This is also the style of programming most amenable to being proven 
correct. Since such a solution can usually be written more simply and 
maintainably, it makes more sense to do it this way. 



  -- Scott

  [1]: https://en.wikipedia.org/wiki/Hoare_logic

[toc] | [prev] | [next] | [standalone]


#28475

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-07-29 03:04 +0000
Message-ID<mp9fr2$mbk$1@dont-email.me>
In reply to#28428
On Wed, 15 Jul 2015 09:59:23 +0200, danca (Daniele Campagna) wrote:

> I wrote this small recursive function to find a node (having a name) in
> a DOM element:

Why not start with document.getElementsByName('<somename>')?

You could also use xpath.

see http://www.sined.co.uk/tmp/xpath_vs_gebn.htm for examples of both.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#28502

From"danca (Daniele Campagna)" <cyberdanny@tiscalinet.it>
Date2015-08-06 10:20 +0200
Message-ID<mpv5e9$g69$1@dont-email.me>
In reply to#28475
Il 07/29/2015 05:04 AM, Denis McMahon ha scritto:
> On Wed, 15 Jul 2015 09:59:23 +0200, danca (Daniele Campagna) wrote:
>
>> I wrote this small recursive function to find a node (having a name) in
>> a DOM element:
>
> Why not start with document.getElementsByName('<somename>')?
>
> You could also use xpath.
>
> see http://www.sined.co.uk/tmp/xpath_vs_gebn.htm for examples of both.
>

Right, but in my particular case I can have different forms in the same 
document with <input> elements having the same name! (Then one of the 
forms is made visible and the others are hidden).
This happens because the element name must be the same name of the field 
in the database. When the submit take place the CGI receives the names 
and updates the corresponding fields.

Dan
-- 
"Everybody should pay taxes with a smile"
I tried, but they want money.

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.javascript


csiph-web