loading...

re: JavaScript: How to Remove Duplicate Values from Arrays VIEW POST

FULL DISCUSSION
 

Surely Set must be faster - the whole thing is an O(N) operation with Set, but it's getting towards O(N**2) with anything that has to scan like includes or filter... (The reduce version is better than O(N**2) as it is only having to test the currently created unique list)

I decided to see just how different it is. In this example on jsperf, you can see that the "reduce" is around 180x slower and the indexOf even worse at almost 380x slower. Also the version of "reduce" I've used is slightly faster than your one shown, because it doesn't make an array on each loop and just modifies the one being created.

 

Hi, your benchmark is wrong for indexOf where you are taking all duplicate values instead of unique ones (wrong comparison). Those approaches will perform differently depending on duplicates density - reduce is not necessarily faster.

Both versions are O(n^2).

 

Thanks for throwing together that jsperf test Mike!

Set does appear to be much faster, which is fantastic because the syntax is so much easier to grok (for me, at least).

 

Yes, nice that good syntax has great performance haha! Not a normal happy thing that I find! (Like the immutability is a nicer syntax but comes with a penalty).

code of conduct - report abuse