Suggestions for Set

Nicholas C. Zakas standards at nczconsulting.com
Tue Oct 2 09:43:29 PDT 2012


On 10/1/2012 6:43 PM, Brendan Eich wrote:
> Nicholas C. Zakas wrote:
>> I've been playing around with the Set prototypes in Firefox and 
>> Chrome (knowing full well that they are incomplete and the spec 
>> hasn't been finalized yet), and I wanted to share a couple of learnings.
>
> Thanks!
>
>> First, since a Set can be initialized with an array, it stands to 
>> reason that you should be able to easily get an array back from the 
>> set. However, there is no way to do that right now short of using a 
>> loop and manually constructing an array. I would really like to see a 
>> *Set.prototype.toArray* method to easily change the Set back into an 
>> array. A simple use case would be de-duping an array:
>>
>>     function dedupe(array) {
>>         return (new Set(array)).toArray();
>>     }
>>
>>
>
> Array.from is the way we hope to avoid burdening many iterables with 
> "toArray" methods, as Rick pointed out. Ok?

Better than okay, that's great. Having one way to convert iterables to 
an array makes a lot of sense to me.
>
>
>> Second, the lack of visibility into Sets is problematic. A very 
>> simple use case is when I want to see if the Set has been initialized 
>> yet but I can't know what keys might have been used at that point in 
>> time. Firefox implements a *Set.prototype.size* method which is 
>> useful to know what I'm dealing with. However, I'm not sure how 
>> frequently the number of items in the Set actually matters, so a 
>> *Set.prototype.isEmpty* would be equally useful for this case.
>
> +1 on isEmpty().

:D

>
>> Otherwise, I'm finding Sets very useful as a replacement for objects 
>> that I was using for the same purpose.
>
> Cool. Have you tried Maps for those cases where you want value -> 
> value mappings?

Not yet, that's next on my list.

>
> /be

-- 
___________________________
Nicholas C. Zakas
http://www.nczonline.net



More information about the es-discuss mailing list