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.
According 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].
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
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).
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.
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.
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?
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.
[+] [-] esprehn|1 year ago|reply
[+] [-] mcphage|1 year ago|reply
[+] [-] bsmth|1 year ago|reply
[+] [-] dmix|1 year ago|reply
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
[+] [-] afiori|1 year ago|reply
[+] [-] orangepanda|1 year ago|reply
What optimisations allow that? As sets in javascript maintain insertion order, aren’t lookups O(n) ?
[+] [-] Fishkins|1 year ago|reply
0: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
1: https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHa...
[+] [-] augusto-moura|1 year ago|reply
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
[+] [-] philipwhiuk|1 year ago|reply
[+] [-] tobz1000|1 year ago|reply
Maybe we'll see it one day.
[+] [-] throw156754228|1 year ago|reply
[+] [-] bavell|1 year ago|reply
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
[+] [-] pipeline_peak|1 year ago|reply
Result : [-2,-7,2,6]
Ladies and Gentlemen, the lingua franca of web programming.
[+] [-] leshenka|1 year ago|reply
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
`[6,-2,2,-7].sort( (a,b) => a-b)`
[+] [-] mock-possum|1 year ago|reply
[+] [-] evilduck|1 year ago|reply
[+] [-] twosdai|1 year ago|reply
[+] [-] zarzavat|1 year ago|reply
Some properties cannot be deleted, and some cannot be modified:
Sets also allow you to have sets of other types rather than just strings and symbols.[+] [-] postalrat|1 year ago|reply
[+] [-] moritzwarhier|1 year ago|reply
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
[+] [-] smartplaya2001|1 year ago|reply
[+] [-] mrkeen|1 year ago|reply
Do Java next!