Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #28490
| Newsgroups | comp.lang.javascript |
|---|---|
| Date | 2015-07-30 22:09 -0700 |
| References | <mo53os$am9$1@dont-email.me> <4508534.oS5OQyzpKv@PointedEars.de> <4b1cb0ba-f0b0-4b25-a1aa-692c30e69833@googlegroups.com> <mol6so$48v$1@dont-email.me> <1625412.DiW0shChWm@PointedEars.de> |
| Message-ID | <73f72c73-d67d-4ea0-8771-e8ce5908e3de@googlegroups.com> (permalink) |
| Subject | Re: Recursive function to find a node in the DOM |
| From | Scott Sauyet <scott.sauyet@gmail.com> |
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
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web