On 20/12/05, Gregory Hill <[EMAIL PROTECTED]> wrote: > He's just > copying a value to a local variable and using that instead and there's a > big speed difference?
"foo.bar" or even "foo.bar.baz.qwertz" are more expensive to lookup than "temp". So if you have to use the former multiple times (e.g. in a loop), it is faster to create a local variable to make the long lookup only once. > I could understand if .length > were a function, and had to recalculate the value each time, but it's > just an attribute. Depends. There is another effect here. The array could really be not an array, but an NodeList (like element.childNodes or a list returned by document.getElementsByTagName() ). See http://www.w3.org/TR/DOM-Level-2-Core/ NodeLists are live. That is: If you have var t = element.childNodes and you remove a child from the element by using element.removeChild(), t will follow this change. So any lookup into a NodeList is quite expensive, as the DOM is involved. So indeed, .length has to be calculated on each access (The JS engine of the browser might remedy this through caching - See quirksmode.org for performance comparisions regarding DOM access/manipulation). A nice prototype.js way to create an regular Array of nodes from a NodeList is var arr = $A(nodelist) _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs