8 ms·
New JavaScript Set Methods
- bsmth 2y agoBlog post author here in case there's any feedback or questions. Thanks for sharing :)
- dmix 2y agoThanks 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.
- bsmth 2y agoThat's a good idea. I'll have a think about a JS only example, although the reference pages have some, they log results to the console and so I thought some HTML might be interesting to see. Maybe it's interesting for you: the method pages have compat data at the bottom so you can see what's supported and in which browser release.
- orangepanda 2y ago> 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) ?
- chiefjosh 2y agoProbably a tree set, with O(log(n)) lookups
- Fishkins 2y agoAccording to the Mozilla docs, O(n) lookups would actually violate the spec [0]. As a sibling comment says, a tree set is one option to satisfy the spec. Another is a linked hash set, which would have O(1) lookups [1]. 0: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#description https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... 1: https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashSet.html https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHa...
- augusto-moura 2y agoIt 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
- bavell 2y agoPerhaps 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 2y agoAgree, a main benefit of using these would be performance gains.
- twosdai 2y agoHuh. 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.
- smartplaya2001 2y agoDitto!!!
- zarzavat 2y agoObjects 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.
- threatofrain 2y agoIf you do use objects I might suggest Object.create(null).
- tracker1 2y ago+1 Was going to be my response as well...
- kevincox 2y agoYup. I always use Set and Map over objects. They just contain way fewer surprises and the APIs actually work as you would expect. I only use Objects for "struct" like objects with a known set of keys that are used by name. It is also nice because it conveys more intent to the reader.
- moritzwarhier 2y agoIt'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?
- mcphage 2y agoOh, that's a relief. Having "set" objects in JS, without having any of the standard set operations, didn't make a lot of sense.
- philipwhiuk 2y agoIt's a pity that you can't implement a function in an object to consider it equal to another object with the same member values.
- tobz1000 2y agoThere is a years-old proposal for deeply-comparable types: https://github.com/tc39/proposal-record-tuple https://github.com/tc39/proposal-record-tuple Maybe we'll see it one day.
- afiori 2y agoI highly doubt this will go anywhere, it is much more likely to get some custom equality in data structures. For example I would like a Map<array, any> that compare keys by shallow equality, so to behave like multidimensional tables
- throw156754228 2y agoThis'll help with AoC. Just need sensible tuples now and we'll be golden. See you in ten years.
- esprehn 2y agoI'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.
- wruza 2y agoIt was replaceAll in 2021 and now this! Who knows what we’ll get in 2030. Maybe Array.prototype.last() even.
- pipeline_peak 2y ago[6,-2,2,-7].sort() Result : [-2,-7,2,6] Ladies and Gentlemen, the lingua franca of web programming.
- rk06 2y agoBecause js sort works on string. for numeric sort try: `[6,-2,2,-7].sort( (a,b) => a-b)`
- mock-possum 2y agoIt 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.
- leshenka 2y agonew 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).
- evilduck 2y agoSomeone didn't read the docs.
- mrkeen 2y agoDamn, in 2024? Do Java next!