top | item 40772387

New JavaScript Set Methods

92 points| soheilpro | 1 year ago |developer.mozilla.org

38 comments

order
[+] esprehn|1 year ago|reply
I'm very excited this finally shipped everywhere! Prior to this Set wasn't much more than a Map<any, true> since it had no actual Set operations.
[+] mcphage|1 year ago|reply
Oh, that's a relief. Having "set" objects in JS, without having any of the standard set operations, didn't make a lot of sense.
[+] bsmth|1 year ago|reply
Blog post author here in case there's any feedback or questions. Thanks for sharing :)
[+] dmix|1 year ago|reply
Thanks for the post. One thing, I wanted to test this in chrome and I realized your examples are all based on a larger Playground with a test HTML doc.

Maybe one self-contained example w/ a new function you can copy/paste into the console to play around with would be cool.

I was mostly using it as a test to see if it worked in Chrome so I could start using it.

[+] wruza|1 year ago|reply
It was replaceAll in 2021 and now this! Who knows what we’ll get in 2030. Maybe Array.prototype.last() even.
[+] afiori|1 year ago|reply
It should be available as .at(-1)
[+] orangepanda|1 year ago|reply
> It's also typically faster to check if an element is in a set as opposed to in an Array

What optimisations allow that? As sets in javascript maintain insertion order, aren’t lookups O(n) ?

[+] augusto-moura|1 year ago|reply
It is usually implemented by a linked list + hash set (Java call this, drumroll... LinkedHashSet).

Because we are not adding elements in the middle of the list, only the end (we only care about insertion order), `add` is still O(1). `has` searches on the HashSet so it is also O(1). Delete is a bit more complex, you need to keep the list node reference on the set node, and then just splice it from the list. So O(1) as well

Of course this takes a lot more memory, but I think it usually pays off to have consistent ordering for sets, undefined/non-deterministic behavior is a virus and it spreads very quickly

[+] chiefjosh|1 year ago|reply
Probably a tree set, with O(log(n)) lookups
[+] throw156754228|1 year ago|reply
This'll help with AoC. Just need sensible tuples now and we'll be golden. See you in ten years.
[+] bavell|1 year ago|reply
Perhaps it's asking too much but it would be great to see performance comparisons of these new set methods versus naive JS.

Not sure when I'll have a chance to use them but seems pretty comprehensive in covering all the basics.

[+] captaincaveman|1 year ago|reply
Agree, a main benefit of using these would be performance gains.
[+] pipeline_peak|1 year ago|reply
[6,-2,2,-7].sort()

Result : [-2,-7,2,6]

Ladies and Gentlemen, the lingua franca of web programming.

[+] leshenka|1 year ago|reply
new Int32Array([2,7,-2, -7]).sort()

Result: Int32Array [-7, -2, 2, 7] (4)

The problem is you can put anything in array and JS can't really check that "everything here is a number", so it uses string as a common type because everything can be converted to string.

we need proper typed arrays, like Int8/16/32Array but with any class, not just int. The class itself will then define how to compare instances (python's __gt__ and so on).

[+] rk06|1 year ago|reply
Because js sort works on string. for numeric sort try:

`[6,-2,2,-7].sort( (a,b) => a-b)`

[+] mock-possum|1 year ago|reply
It would be cool if an array of numbers was sorted by numeric value by default, instead of the values being sorted alphanumerically, yes, that’s a gotcha.
[+] evilduck|1 year ago|reply
Someone didn't read the docs.
[+] twosdai|1 year ago|reply
Huh. I might actually start using sets over just simple objects. Never found a great reason to use them before these methods, other than to show off.
[+] zarzavat|1 year ago|reply
Objects not really safe to use as sets:

    const x = {};
    ‘constructor’ in x // true
    x[‘constructor’] === Object // true
You have to use hasOwnProperty and people don’t remember to do this.

Some properties cannot be deleted, and some cannot be modified:

    delete x.constructor;
    x.constructor === undefined // false
    x.__proto__ = undefined;
    x.__proto__ === undefined // false
Sets also allow you to have sets of other types rather than just strings and symbols.
[+] postalrat|1 year ago|reply
You can use objects and other things as keys for a Set or Map. As far as I know you can't do that for objects.

    const s = new Set();

    const a = {}, b = {};

    s.add(a);

    s.has(a); // true

    s.has(b); // false


    const o = {};

    o[a] = true;

    o[a]; // true

    o[b]; // true, cause its cast to a string like "[object Object]"
I've also seen Map perform faster than using an object as a Map.
[+] moritzwarhier|1 year ago|reply
It's a pretty straightforward way to do things like avoiding duplicates or checking if an item is part of a collection without using quadratic loops or string-keyed dictionary objects.

It's O(1) for the latter (edit: I stand corrected, but probably close, as per spec).

Conversion to and from arrays even preserves order.

So I don't see these two use cases as "showing off", why should anybody do that?

[+] DylanSp|1 year ago|reply
I've used them occasionally to clarify the semantics of collections; I like being able to express that some collection of values is unordered and has only unique elements.
[+] mrkeen|1 year ago|reply
Damn, in 2024?

Do Java next!