r/learnjavascript • • May 14 '21

Remove Duplicate Value from array

Post image
234 Upvotes

89 comments sorted by

View all comments

Show parent comments

16

u/[deleted] May 14 '21

It's less performant, but not dramatic. Whether or not this is faster is an implementation detail in the runtime essentially. It is a lot more concise though, and hard to get wrong. 99/100 times i'd just import uniq / uniqBy from lodash anyway.

-26

u/StoneCypher May 14 '21

It's less performant, but not dramatic.

It's the construction and iteration of an entirely unnecessary unordered container

It's not clear how you'd make this "dramatically worse" that this isn't, other than to just make it wrong, or do something nonsensical

Yes, it's dramatic. It's large constant big o lg n twice plus small constant big o n lg n for literally no reason.

.

Whether or not this is faster is an implementation detail in the runtime essentially

this isn't even close to correct

.

It is a lot more concise

Not than the good answer

.

99/100 times i'd just import uniq / uniqBy from lodash anyway.

Wow, please don't give advice anymore

I've had to update my libraries 11 times this week because people like you imported lodash or underscore for no good reason, and they're full of bugs and security problems

It's so hard to just have decent testing or doc generation because of things like this :(

This is the mindset that left a serious chunk of the internet broken over padLeft

13

u/[deleted] May 14 '21

this isn't even close to correct

It's very correct, i welcome you prove otherwise.

Not than the good answer

Please provide the good answer then

Wow, please don't give advice anymoreI've had to update my libraries 11 times this week because people like you imported lodash or underscore for no good reason, and they're full of bugs and security problems

I don't think that's correct. If you honestly believe you write fewer bugs and more secure code than lodash, please provide some kind of proof, because you're likely wrong.

Yes, it's dramatic.

No it's not dramatic.

I did a jsbench in chrome test at various array sizes, 10, 100, 1000, 10_000, 100_000, ..., on three different approaches - Set, filter + indexOf, filter + object.

For small arrays filter + indexOf is the fastest. For very large arrays filter + object is fastest. For the middle Set / filter + object are not significantly different.

And of course the filter versions could be written with a loop and be faster.Overall though a pointless optimization unless perf testing your app reveals that you need it. Lodash takes size of array into account and chooses approach based on that, so you get the optimization for free.

-11

u/StoneCypher May 14 '21

Whether or not this is faster is an implementation detail in the runtime essentially

this isn't even close to correct

It's very correct, i welcome you prove otherwise.

What's there to disprove? You just said randomly "this is an implementation detail." You didn't make a technical claim.

This is like saying "node.js is angry which is an implementation detail" and when someone says that doesn't make sense saying "prove me wrong"

No, dear heart. You prove yourself right. Other people don't prove you wrong.

"An implementation detail" means that something is a decision for the underlying machine, that is out of the developer's hands.

You just said "the shape of the code that the developer chooses to write is not up to the developer."

That's absolute nonsense.

Every part of that code is hard-defined by the specification. None of it is an implementation detail.

That phrase has a meaning. You're hard wrong.

 

Please provide the good answer then

There's tons of them. I prefer one that you probably won't prefer, because this should be a tiny library function tested and put away in some module somewhere, and even though it's a one-liner nobody will ever see, you're crying out for "simplicity"

The function can't be well written; the name is ambiguous. You need to provide a stub that is clear about what is being done with holes, undefined, and symbols.

Holes are a problem because the various implementations mostly lose them. However, under most interpretations of the ECMA-262 concept of an lvalue, they need to be kept.

Symbols are a problem because the concept of uniqueness is a conceptual error around them in general. Two copies of the same symbol are not equal. equivalence is broken for them on purpose, and uniqueness is conceptually defined in terms of equivalence.

undefined is a problem

.

No it's not dramatic. [...] I did a jsbench

I see that you:

  • didn't give numbers

  • didn't show your test code

  • didn't share the share-able bench in case there were mistakes

  • didn't cover correctness topics

  • didn't specify whether your implementations gather the same results

  • said there were differences in speed but also argued against my saying there were differences in speed

  • said "the one in the middle isn't too far from one side" which is obviously going to be the case because the one side is the other good approach

  • compared several tiny sizes and referred to one of them as large

  • used a benchmarker that can't see micro-timing at all

  • ignored the o claim

Compelling stuff

Wanna show us this bench?

I benched it too and it turns out the solution I don't like took infinity hours and the solution I do like gave me a pony

 

For very large arrays

100k is not a very large array. Lawdy.

Node authors regularly deal with two or three orders of magnitude larger than this.

The US map on d3 has more than 50x this many SVG tags. SVG tags are not simple.

 

I don't think that's correct. If you honestly believe you write fewer bugs and more secure code than lodash, please provide some kind of proof, because you're likely wrong.

I didn't say anything like this.

What I did say is "don't involve a specific library that's had three criticals in the last two weeks to get away from a simple one-liner."

You know, the three one-liners you claim to test in a jsbench later?

It's very easy to avoid lodash in favor of the standard library.

I didn't say my code has fewer bugs. I said the standard library does.

I don't need to prove that. It's tautological. Anything written using the standard library, such as lodash, already includes every bug in the standard library.

Do I believe that chrome has fewer security bugs than lodash, which had three criticals in the last week?

Emphatically yes.

You want me to accept a benchmark you wrote in 15 minutes, but also you're skeptical that Chrome has fewer security bugs than lodash, one of the least secure libraries on NPM 😂

 

Overall though a pointless optimization

It's annoying when novices respond to "don't do it that way, it's bad for a bunch of reasons including correctness and performance" with "YoU'rE jUsT mAkInG uNnEcEsSaRy OpTiMiZaTiOnS"

Correctness is not an optimization

lodash does what I believe to be the wrongest things with uniqueness around holes, and around Symbols (I do agree with what it does around undefined)

I doubt you even thought to check

 

Lodash takes

Lodash has had three unrelated security vulnerabilities marked critical or worse in the last two weeks

I cannot take seriously the opinions of someone who keeps pushing for one-liners to be replaced with it, after they were told that once already

If you're arguing against maintenance, arguing for things with a history of grim security problems is arguing against yourself

12

u/[deleted] May 14 '21

Please be more concise, I have no interest in countering a proof by verbosity where you've introduced tons of new arguments that had nothing to do with the original post or discussion and contradict your own self many times.

-11

u/StoneCypher May 14 '21

A third time, I'm requesting that you provide the perf you claim to have written

You're the one who brought lodash up, not the original post or discussion

Now that I'm pointing out why adding lodash is a security problem, which is a hard fact but which you refuse to accept, you're babbling on about how everything has to be scoped to the original post or original discussion

Show us your perf that you're trying to stand on as evidence. I'm starting to think it isn't real, because I wrote one that got really different results

1

u/[deleted] May 14 '21 edited May 14 '21

Lodash's vulnerabilities only matter in relation to the code that you write that is allegedly better, since you are advocating to write it yourself rather than let someone else do so.

You yourself said that this is a tiny library function, a one liner, and that you could write just as concisely as [...new Set(arr)], and that it'll be dramatically more performant than the OP on an array with millions of items. But you refuse to do so for some reason. Can't be time since you have the time to repeatedly write large walls of text.

Furthermore I've looked through Lodash CVEs and I see none relating to function in question. The majority of them seem to be prototype pollution from functions that merge objects, which is outside of the scope of deduping an array. So it was a moot point to begin with designed to distract from the actual point.

Finally, since you allegedly have the ability to write code that is faster, safer, more maintainable, and more concise than lodash - why are you so selfish as to keep this code to yourself? Publish it and let the community benefit.

-3

u/StoneCypher May 14 '21

A fourth time, I'm requesting that you provide the perf you claim to have written

2

u/[deleted] May 14 '21

Here i've modified someone else's that you seemed to be interested in, now where's your wonderful implementation?

https://www.reddit.com/r/learnjavascript/comments/nc6i5j/remove_duplicate_value_from_array/gy48wl4?utm_source=share&utm_medium=web2x&context=3

0

u/StoneCypher May 14 '21

That's not a perf. That's you copy pasting someone else's output

Is it fair to suggest that the work you're standing on having done is just you attempting to guess from someone else's work?

 

now where's your wonderful implementation?

Why are you asking? Is it because you believe claiming to have done a perf, then not having done one, is a problem?

Looks like the comment you're responding to is deleted.

Is it a problem that their output is copy pasted from StackOverflow, that you're claiming you did, and that the StackOverflow post is about something else?

→ More replies (0)

2

u/[deleted] May 15 '21 edited Aug 31 '25

[removed] — view removed comment

1

u/StoneCypher May 15 '21

you've quite badly misunderstood the costs

list(set(arr)) means:

  1. Converting the array to a set

    1. Iterate the array

    2. For each array item, make an o(lg n) probe into the new set at increasing by-size cost to determine whether to insert

    3. In most languages, including python (not defined in JS but including both popular JSVMs,) you may need a red-black rebalance after insert, which is also amort o(lg n)

  2. Convert the set to a list

    1. Iterate the set's tree

    2. Allocate and enjoin a ton of list items

  3. Present the list

No, filter isn't o(n^2) in python, or indeed in any language that can't optimize some of the work out (like maybe Haskell could but I doubt it,) and it's not clear why you think that it is. Python doesn't define the complexity of filter, but I've never seen it be anything other than linear over arrays in any language in my entire life (with the notable exception of Visual Basic, which uses the word "array" for something that isn't actually an array.)

You can't actually do the uniqueness thing faster than o(lg lg n), and getting it down that far takes a fairly large amount of knowledge about the data. How did you think you were getting a presence test in linear time? That isn't possible

Can you help me understand why you think filter is o(n^2) over a direct iterable? That doesn't make any sense. It's:

  1. Allocate a new matching-sized container

  2. Iterate the base array o(n) a single time, linearly, in order

    1. For each element, el[i] = function(source[i])

  3. Present the container

Where does the second order n come from? It's o(up-to-n) + o(n), one for the allocate, one for the traversal

1

u/[deleted] May 15 '21 edited Aug 31 '25

[removed] — view removed comment

0

u/StoneCypher May 15 '21

wow you're really going to try to line by line?

The "n not in deduped" is a linear search of deduped.

this is the second n

 

and getting it down that far takes a fairly large amount of knowledge about the data. How did you think you were getting a presence test in linear time? That isn't possible

Please check out the Hash Map chapter of Introduction to Algorithms by CLRS.

Yeah, that just moves the cost from test to insert

Look, if you're going to talk down to people this way, don't ask for explanations from them

 

⁠Present the container

O(n)

😂 no, returning a pointer isn't o(n)

 

What details are you hiding in function?

You're trying to make commentary about container conversion costs by pretending that there's a conversion function?

i'm sorry, but this really isn't worth my time

0

u/[deleted] May 15 '21

[removed] — view removed comment

0

u/StoneCypher May 15 '21

This isn't worth my time.

You're ignoring clearly spelled out mistakes already given to you.

 

What should be worth your time is researching hash maps.

Nobody asked for your advice.

I realize you're trying to win, but there's no fight here. You asked me to explain myself, I did, you tried to argue, and I walked away.

Imagine telling someone returning a container was o(n), not admitting the mistake when it's pointed out, and continuing to try to give someone else a reading list 😂

"No but when you said converting containers, did you mean printing them on screen?"

🤣

Good day.

0

u/[deleted] May 15 '21 edited May 15 '21

[removed] — view removed comment

1

u/StoneCypher May 15 '21

I mean, if you're going to engage, at the very least read what you're responding to.

I'm not going to engage. You ignored the answer you got, and everything since. I've no further interest.

Several messages in a row I've told you so, and told you good day.

Learn to take "no" for an answer more gracefully.

→ More replies (0)

4

u/[deleted] May 14 '21

This is the mindset that left a serious chunk of the internet broken over padLeft

This is also completely incorrect. People using packages was not the root cause of that problem. npm allowing packages to be removed at will was certainly a problem. people counting on npm to be always up was certainly a problem. javascript having a pathetic standard library was mainly to blame.

You weren't even right about a single thing in that massive wall of text which is pretty impressive.

1

u/StoneCypher May 14 '21

This is also completely incorrect. People using packages was not the root cause of that problem

It was literally 100% of the problem. It's a subtraction and a .repeat

 

npm allowing packages to be removed at will was certainly a problem.

This is required both by security common sense basics and by copyright law.

 

javascript having a pathetic standard library was mainly to blame.

It's not Javascript's fault that you don't know how to do this

By the way, Javascript does have this in the standard library directly, it's just called .padStart

Yes, that was already in Javascript before the package you're trying to make excuses for was added

Yes, they could have used a shim or a sham

 

You weren't even right about a single thing in that massive wall of text which is pretty impressive.

Sure thing, Jim

Sorry you don't like evidence or questions you can't answer

 

Can we see your mystery perf that you claimed to write? It's not getting the same results I did, and I'd like to see why