> When iterating over a `source` items, we don't know if an item is a > wrapper or an element.
My _each implementation deals with that case pretty efficiently. The ongoing runtime cost of item() and _each() is pretty much one extra comparison. Before we can do benchmarks, we should ask people for usage guidance. For instance, is the statement "In most cases, some operation needs to be performed on the entire collection right after 'collecting it':" true? It may be, or may not. (In cases where it's not, we can probably give people guidance about honing their CSS...) -- T.J. :-) On Aug 22, 4:37 pm, kangax <[EMAIL PROTECTED]> wrote: > On Aug 22, 10:33 am, John-David Dalton <[EMAIL PROTECTED]> > wrote: > > > T.J. $$W and $$ are the same thing (just named $$W so that we know we > > are talking about the new implementation). > > > I kind of like the idea of lazy wrapping. > > That would speed up the initial selector while offering a one time hit > > later on, > > on only the elements chosen (which is usually all of them being > > iterated over by > > "each" or "include") but for those cases were it iterates against > > "find" it would help because > > the un-iterated items wont be wrapped. Wrapping is by no means as slow > > as extending the elements manually (ala IE), you create an instance > > and assign it the passed element so the speed hit is still minimal. > > John, > > I'm not worried about initial performance "hit". I'm worried about > initial memory "hit". Lazy initialization uses constant amount of > memory - i.e. creating 1 Object object (instance of > Prototype.NodeList) and 1 Array object (actual elements). If you were > to use ".item(n)" afterwords, it would result in creating another > Object object at run-time (i.e. an instance of Prototype.Node). > > The problem here is that accessing 1 item is something that doesn't > happen often. In most cases, some operation needs to be performed on > the entire collection right after "collecting it": > > $$W('h2.toggle').observe('click', function(){ > this.next().toggle(); > > }); > > $$W('tbody tr:nth-child(odd)').addClassName('odd'); > > $$W('#nav li > a').removeClassName('active').first().addClassName('active'); > > // etc. > > I think that, most of the time, the memory consumption difference will > be negligible. The run-time performance hit, on the other hand, is > something that could hog things down. Even if $W were to wrap element > only once, there is still some overhead: > > When iterating over a `source` items, we don't know if an item is a > wrapper or an element. We still need to either explicitly check for > it, or delegate the check to $W: > > */ > $w('show hide update')._each(function(method) { > Prototype.Nodelist.prototype[method] = function() { > for (var i=0, l=this.source.length; i<l; i++) { > // wrap elements at run time > if (this.source[i] instanceof Prototype.Node) { > this.source[i][method].apply(this.source[i], arguments); > } > else { > (this.source[i] = $W(this.source[i])) > [method].apply(this.source[i], arguments); > } > } > } > > }); > > // or delegating > > $w('show hide update')._each(function(method) { > Prototype.Nodelist.prototype[method] = function() { > for (var i=0, l=this.source.length; i<l; i++) { > // wrap elements at run time > (this.source[i] = $W(this.source[i])) > [method].apply(this.source[i], arguments); > } > } > > }); > > // and $W would take care of the rest > > function $W(element) { > if (element instanceof Prototype.Node) { > return element; > } > if (typeof element == 'string') { > element = document.getElementById(element); > } > return new Prototype.Node(element); > > }; > > There is an overhead of an extra function call (which could > potentially be performed thousands of times) and all these > conditionals. > > Anyway, let's wait till we have some benchmarks to support these > claims : ) > > -- > kangax --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Prototype: Core" group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~----------~----~----~----~------~----~------~--~---