On Aug 21, 2:05 pm, Ken Snyder <[EMAIL PROTECTED]> wrote:
> kangax wrote:
> > ...
> > Are we going to make `ListWrapper` pretend it's an array?
> > I agree that it's convenient to be able to access single elements of a
> > list with brackets, but wouldn't exposing all these properties break
> > the integrity/abstraction of a `ListWrapper`?
>
> > jQuery "fills" its instance with such properties and seems to adjust
> > `length` accordingly, but it's far from being an array:
>
> > var j = $('div');
> > j.length; // 14
> > j[14] = 'foo';
> > j.length; // 14 (not 15) - no "magic" length behavior obviously
> > j.push; // undefined
> > j.pop; // undefined
> > j.concat; // undefined
>
> > I think the only array-like method they have is `slice`.
>
> > --
> > kangax
>
> I agree that ListWrapper should not try to be like an array.
>
> In fact, I think it would be faster to have a ListWrapper#item method
> instead of extending the ListWrapper with numbered indexes:
>
> $$W('div p').item(5);
>
> ListWrapper.prototype.item = function(index) {
>   // in this example, the element isn't even wrapped until it is accessed
>   return this.raw[index] ? $W(this.raw[index]) : undefined;
>
> };
>
> Length could be a property like a browser NodeList object, but we might
> find more consistency with a ListWrapper#size method instead.

Good points, Ken.

This made me think about lazy initialization of NodeListWrapper's. The
two options seem to be:
1) store an array of "pure" elements and turn them into wrappers "on
the fly"
2) turn elements into wrappers when instantiating `NodeListWrapper`
and then return these (already "prepared") wrappers.

I'm in favor of putting all the hard work on constructor (#2) so that
`NodeListWrapper` methods don't have to spend time wrapping elements.
This way, you could create `NodeListWrapper` outside of intensive
areas of application (such as frequent event handlers) and have a
higher performance later on:

Btw, the performance of wrappers looks very optimistic:

// 217 elements total; "base_test.html" from the prototype's "test/
unit"
// FF 3.0.1 / OSX 10.5

var elements = $$('*'), nodeListWrapper = $$W('*');

console.time('elements - invoke');
for (var i=0; i<100; i++) {
  elements.invoke('show');
}
console.timeEnd('elements - invoke');


console.time('elements - each');
for (var i=0; i<100; i++) {
  elements.each(Element.show);
}
console.timeEnd('elements - each');

console.time('wrapper - each');
for (var i=0; i<100; i++) {
  nodeListWrapper.show();
}
console.timeEnd('wrapper - each');


console.time('wrapper - for');
for (var i=0; i<100; i++) {
  nodeListWrapper._show();
}
console.timeEnd('wrapper - for');

/*
elements - invoke: 718ms
elements - each: 282ms
wrapper - each: 250ms
wrapper - for: 220ms
*/

--
kangax
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to