Suggestions for Set

Brendan Eich brendan at mozilla.org
Mon Oct 1 18:43:08 PDT 2012


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?


> 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().

> 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?

/be


More information about the es-discuss mailing list