Re: toArray() method for array-like DOM list objects

On 12/22/11 1:51 PM, Marat Tanalin | tanalin.com wrote:
> At least since it uses some function with dummy argument

What dummy argument?

> to do something that slice function is not intended to

The slice function is purposefully generic.  That means it's intended to 
take any array-like as |this| and produce an array.

> and that purpose is no-so-clear from the script code.

That's a reasonable complaint.

> There was also information from jQuery developers (cannot find a link unfortunately) that this method works even slower that manual iterating item-by-item.

That would simply be an implementation bug.  One which toArray may well 
share if it were implemented.

I have seen no such bug reports from jquery developers (though there 
were recent changes to Spidermonkey's slice() implementation to make it 
much faster on nodelists, when someone else complained about it), so 
either it wasn't a big issue for them or they for some reason didn't 
decide to report it.

> toArray() would be much more clear, straightforward, and fast way to achieve exactly converting DOM list to Array.

I'm almost willing to buy "clear" and "straightforward".  Except that it 
would only work for some objects but not others, whereas slice() works 
for any array-like.  So you'd have to know exactly what sort of object 
you have to use it; in practice this would be a huge pain I suspect.

A much better API, if it could be done, would be an Array constructor 
taking an arraylike object...  But that would need to be done in 
ECMAScript, of course.

As for "fast", that's a matter of implementation as I said above. 
Nothing makes things magically fast; they end up fast when someone makes 
sure to write fast code.

-Boris

Received on Thursday, 22 December 2011 19:06:15 UTC