r/javascript • • Mar 18 '19

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

385 Upvotes

422 comments sorted by

View all comments

Show parent comments

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.