r/javascript • • Mar 18 '19

What's your favorite, most useful javascript one-liner?

385 Upvotes

422 comments sorted by

View all comments

339

u/Drugba Mar 19 '19

Saw this one on here just a few weeks ago.

[... new Set(arr)]

Basically a super short way to remove any duplicates from an array of primitives.

17

u/[deleted] Mar 19 '19

Is order guaranteed to be maintained with the Set data structure here?

26

u/Drugba Mar 19 '19 edited Mar 19 '19

No, it's not.

/u/BTOdell is correct. Insertion order is preserved when you spread a set.

26

u/BTOdell Mar 19 '19

The Set object in JavaScript preserves insertion order. So if you create a Set from an array, it will preserve the order when you spread it back out (but without duplicates).

9

u/Drugba Mar 19 '19

Oh, wow. You are totally correct.

I was under the impression that, because sets (the data structure) do not have an order, you wouldn't be able to guarantee order when you spread it, but that to totally wrong.

7

u/NoInkling Mar 19 '19

Worth noting that if there were duplicates in the original array, the position for an element will be based on its first occurrence.

> Array.from(new Set([1,'foo',2,'foo']))
[1, "foo", 2]

13

u/[deleted] Mar 19 '19

[...new Set(arr.map(x => JSON.stringify(x))].map(x => JSON.parse(x))

Same thing, but with arrays of objects. Apparently probably deterministic but maybe not it depends ohgodhelpme

18

u/cheekysauce Mar 19 '19

It'll barf on circular references unfortunately.

41

u/dodeca_negative Mar 19 '19

Don't we all

3

u/NoInkling Mar 19 '19 edited Mar 19 '19

It will have issues with anything non-serializable, explicitly set undefineds will disappear on objects (if that matters), and undefined inside arrays will turn into null. And maybe some other edge cases I'm forgetting.

4

u/SoInsightful Mar 19 '19

It will implode on undefined, circular references, and most appallingly, objects with different key orders. Not really obscure edge cases. People really need to stop using the JSON trick.

2

u/noruthwhatsoever Mar 19 '19

I like this one, very clever

1

u/tontoto Mar 19 '19

I sort of wish I could define what constitutes equality with a Set (random PTSD flashback of hashCode and equals in Java) er maybe I don't...

1

u/GBcrazy Mar 20 '19

You don't need to create an arrow function each time you want to call only one function. This works:

[ ... new Set(arr.map(JSON.stringify))].map(JSON.parse)

You would need though if the function depended on the context (the JSON object in this case isn't needed for the function itself), or you could .bind it too

3

u/oorza Mar 19 '19

Copying your entire data structure twice to remove duplicates is good to you?

5

u/Drugba Mar 19 '19 edited Mar 20 '19

It depends. If I've got a list of 10 items, this is totally fine. Sure it's I'm using more space than I need, but it's pretty negligible and as long as it's not in the global context, the memory is going to be released soon enough anyway. If I've got a list of two million items, of course this is stupid.

One liners often trade performance for terseness, which is what this does. The question asked wasn't "Write the most performant code you can", it was "what is your favorite one liner" and that's what this is.

If you're writing JS code where literally every single byte counts, more power to you, but most people aren't. This is perfectly acceptable for small arrays.

1

u/NoInkling Mar 19 '19 edited Mar 20 '19

Ideally sets would have some useful methods so you could work with them directly more often. See proposals: 1 2 3. But for now it's acceptable for my personal use cases.

1

u/BrQQQ Mar 23 '19

Yes. Unless you’re writing unusually high performance code or have huge collections, this matters nothing.

2

u/noruthwhatsoever Mar 19 '19

I like this one a lot, I use it regularly

-4

u/CallMeOutWhenImPOS Mar 19 '19 edited Mar 19 '19

So basically removing the intersection of an array for the resulting unordered pair cartesian products? I wonder what the big O of such a function is.

1

u/kryptogalaxy Mar 19 '19

If the Set uses hashing, then it's O(n). It will iterate through the list and add each item to the set. Adding an item requires checking for uniqueness which can be O(1) assuming the underlying data structure is some kind of hash table. If it's not, then it would have to iterate through the current items. This would be O(n2).

→ More replies (4)