Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #18047
| Message-ID | <2382244.sAQUXPKQoY@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2013-01-10 01:36 +0100 |
| Subject | Re: Request for opinions on my newbie approach |
| Newsgroups | comp.lang.javascript |
| References | <kcilrc$vt8$1@dont-email.me> <10136443.OnBuAidq6I@PointedEars.de> <a2hre85m1aojpu1l2a46uqbklvnhq7ih3h@4ax.com> <kckrav$41u$1@dont-email.me> |
| Followup-To | comp.lang.javascript |
Followups directed to: comp.lang.javascript
Leonardo Azpurua wrote:
> "Jim T." […]:
>> On Wed, 09 Jan 2013 04:31:58 +0100, Thomas 'PointedEars' Lahn
>>>> for (var i = 0; i < f.elements.length; i++)
>>>
>>> for (var i = 0, len = f.elements.length; i < len; ++i)
>>
>> Why? Because it's faster? First, modern JS interpreters will probably
>> already do this optimization. Second, unless the form has a million
>> elements it will make no noticeable difference. Cleaner code is better
>> than pointless "optimization".
>
> In this particular case, Thomas is absolutely right.
>
> Good style -hence good code- comes from good habits.
>
> And good habits come from the strict observance of best coding rules:
> unless a function value may change during the loop execution, avoid using
> the function as a limit for the loop.
>
> Thomas suggestion is an improvement independently of the context. My
> original code may be fine in the given context, but is bad code.
^^^^^^^^
> And bad code must be corrected.
>
> It is not a trivial optimization, but an important correction of the
> style.
Not so fast :)
This discussion, and the direction it is taking, reminds me that it is
actually very important to ask the question “Why?”; to understand, to be
conscious of, *why* one does things, and continuously question one's
(design) decisions (and that of others). Never assume that you know
everything, or cannot improve anymore. For that matter, never think in
black-and-white categories like “good” and “bad”. For example, “best coding
rules” can easily turn out to be just bad habits other people had because
you did not allow yourself to think out of the box.
The reason *why* this style is preferred (by me) is that it is more runtime-
efficient (as demonstrated often before to be just coincidence). As you
observed correctly (but perhaps unconsciously), the reason *why* it is
actually more runtime-efficient here is (beyond any possibly dubious
benchmark results) that the “length” property *here* yields (through a
getter *function*) the number of items in an *DOM* (*host*) object¹
implementing the HTMLCollection interface (which is probably implemented as
a linked list and a hash table; see below why):
<http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-40002357>
That value can *not* be cached by the script engine because those host
objects are “live” (ibid.): the number of items may change during the loop.
So each property access must invoke said getter. For example, consider this
(not the most efficient way to do it, I know, but it proves my point
nicely²):
while (node.childNodes.length > 0)
{
node.removeChild(node.lastChild);
}
(BTW: This is a standards-compliant equivalent of “node.innerHTML = "";”)
Insofar there *is* a CAVEAT attached to this optimization: One must be sure
that the number of items does not change while the loop is executed. That
is usually either true, or unimportant if false.
Otherwise too few or to many items of the NodeList or Collection will be
attempted to be accessed, whereas the latter, if unchecked, can lead to a
runtime error.³
_______
¹ Like all host objects, it is _not_ part of any programming language it is
accessed with.
² For those interested,
while (node.lastChild)
{
node.removeChild(node.lastChild)
}
is probably among the most efficient implementations.
³ Because “undefined has no properties” or, IOW, cannot be converted from
the primitive Undefined type to the Object type.
--
PointedEars
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread
Request for opinions on my newbie approach "Leonardo Azpurua" <leonardo@exmvps.org> - 2013-01-08 22:24 -0430
Re: Request for opinions on my newbie approach Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-09 04:31 +0100
Re: Request for opinions on my newbie approach "Leonardo Azpurua" <leonardo@exmvps.org> - 2013-01-08 23:34 -0430
Re: Request for opinions on my newbie approach Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-09 05:14 +0100
Re: Request for opinions on my newbie approach Jim T. <x@y.z> - 2013-01-09 14:32 -0500
Re: Request for opinions on my newbie approach Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-09 20:41 +0100
Re: Request for opinions on my newbie approach Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-09 21:03 +0100
Re: Request for opinions on my newbie approach Jim T. <x@y.z> - 2013-01-09 15:11 -0500
Re: Request for opinions on my newbie approach Luc Yen <luc@goal.tw> - 2013-01-09 14:38 -0800
Re: Request for opinions on my newbie approach "Leonardo Azpurua" <leonardo@exmvps.org> - 2013-01-09 18:10 -0430
Re: Request for opinions on my newbie approach Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-10 01:36 +0100
csiph-web