New JavaScript Set Methods
developer.mozilla.org
developer.mozilla.org
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.
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.
What optimisations allow that? As sets in javascript maintain insertion order, aren’t lookups O(n) ?
0: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
1: https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHa...
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
Maybe we'll see it one day.
For example I would like a Map<array, any> that compare keys by shallow equality, so to behave like multidimensional tables
Not sure when I'll have a chance to use them but seems pretty comprehensive in covering all the basics.
Result : [-2,-7,2,6]
Ladies and Gentlemen, the lingua franca of web programming.
`[6,-2,2,-7].sort( (a,b) => a-b)`
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).
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.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?
The main "result" of the interview shouldn't just be "does the code work". The process is far, far more important for understanding how you write code.
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.Do Java next!