r/learnjavascript • • May 14 '21

Remove Duplicate Value from array

Post image
234 Upvotes

89 comments sorted by

View all comments

20

u/slowthedataleak May 14 '21

Does this perform better than filter()?

78

u/StoneCypher May 14 '21

no, dramatically worse.

this is actually a terrible approach, and a big part of the reason why you shouldn't learn javascript from memes by dev.to spammers on reddit

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.

→ More replies (46)

2

u/amoliski May 14 '21

It's only terrible if you care about performance. If you do the operation a single time in response to a user clicking a button, who cares if it takes 0.01s longer to complete?

It's a bit more readable than the faster filter method, and that by itself has some value.

→ More replies (1)

4

u/suprabha-supi May 14 '21

It does not perform better than filter, shared one more way to remove duplicate values from an array

2

u/47milliondollars May 14 '21

Would filter even work? Or do you have to use reduce to check if the value exists yet in an accumulator?

7

u/space_scarab May 14 '21
arr.filter((el, index) => {
    return arr.indexOf(el) === index;
});

11

u/47milliondollars May 14 '21

Oh holy shit, never thought about this. Looks like the indexOf is finding the first match, and so the filter drops anything of the same value that isn't at the first detected index? Pretty neat.

10

u/[deleted] May 14 '21 edited May 14 '21
arr.filter((el, index) => arr.indexOf(el) === index);

Works too!

(but worth saying that shorthand isn't always the most readable and shouldn't be your default approach!)

3

u/[deleted] May 15 '21

Honestly that shorthand is way easier to parse - for me at least

2

u/StoneCypher May 14 '21

is there any difference there other than switching to an arrow?

0

u/[deleted] May 14 '21

[deleted]

-5

u/StoneCypher May 14 '21

Thanks, I didn't ask what an arrow was.

What I asked was "is that the only change you made"

Notably, you shouldn't have that "auto-corrected," because arrows and standard functions differ in how they bind this. That should be hand-corrected. In many cases in practice, this will damage transitional-style classes.

7

u/47milliondollars May 14 '21

Dude, way to be a jerk to someone who kindly took the time to respond to that question (why would they think you were asking if they made any other changes when you're looking right at the 1-3 lines of code?). Also, their response was perfectly accurate for this code example we are looking at, the context of "this" is totally irrelevant here.

-3

u/StoneCypher May 14 '21

(why would they think you were asking if they made any other changes when you're looking right at the 1-3 lines of code?).

The point of the original question was to ask why this person is holding up a non-change as a change. I was trying to make sure I wasn't overlooking something else more substantive.

 

Also, their response was perfectly accurate for this code example we are looking at, the context of "this" is totally irrelevant here.

Yes, I'm ... kind of relying on that.

Literally the only difference between the two pieces of code is something that is totally irrelevant here.

It's like taking out the newlines and "proposing it as an alternative." Literally.

→ More replies (2)
→ More replies (1)
→ More replies (1)

3

u/learning_account_01 May 14 '21

Man. I wish i can think like this. How do you do ? I was stuck at this smilar knida problem (find if array contains duplicate value or not and just return true or false.) and it never clicked me.

4

u/StoneCypher May 14 '21

Man. I wish i can think like this. How do you do

it's the accepted answer from the first result on googling "javascript unique"

https://stackoverflow.com/a/14438954/763127

it's pretty standard in the javascript community

4

u/learning_account_01 May 14 '21

Oh that shows how noob I am. I have lot to learn indeed. Thanks.

3

u/amoliski May 14 '21

Even without SO giving you that answer, it just takes practice and repetition to figure these things out on your own.

If you use indexOf enough times, you eventually learn (and reinforce in your brain) that indexOf returns the first match.

If you use filter enough, the mechanics become second nature.

Once you have both ideas cemented through practice, the connections just start to happen for you.

2

u/47milliondollars May 14 '21

Don't worry fren, a huge part of coding is googling stuff, you're not expected to think up this shit on your own :)

2

u/learning_account_01 May 14 '21

you're not expected to think up this shit on your own

Well thank god otherwise I can't even dare to dream of becoming a programmer some day.

→ More replies (1)

0

u/[deleted] May 14 '21

[deleted]

2

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

emojis.concat(emojis);

this doesn't modify the original array and you aren't saving the result...

You should also use different files to run each algo just so it's isolated and the second one isn't benefiting from JIT optimization from the first.

Another thing, you don't want to include the time to generate the array in your measurement since you're really interested in the deduping.

I did it with the modifications suggested and Set version is much faster than indexOf as the array gets larger, which isn't surprising.

Here's output for the 10_000 loop

~/perf
❯ node -v
v15.14.0

~/perf
❯ time node indexOf.js
Took 3.4395 milliseconds to generate:
node indexOf.js  1.14s user 0.89s system 110% cpu 1.849 total

~/perf
❯ time node indexOf.js
Took 3.4735 milliseconds to generate:
node indexOf.js  1.15s user 0.91s system 110% cpu 1.862 total

~/perf
❯ time node indexOf.js
Took 3.4432 milliseconds to generate:
node indexOf.js  1.15s user 0.92s system 111% cpu 1.857 total

~/perf
❯ time node set.js
Took 0.9747 milliseconds to generate:
node set.js  1.18s user 0.90s system 111% cpu 1.869 total

~/perf
❯ time node set.js
Took 0.8821 milliseconds to generate:
node set.js  1.20s user 0.91s system 111% cpu 1.890 total

~/perf
❯ time node set.js
Took 0.9954 milliseconds to generate:
node set.js  1.20s user 0.90s system 111% cpu 1.886 total

1

u/StoneCypher May 14 '21

ten thousand is far too small of a sample. you're more measuring context switches than anything

please bump that up to at least 1 million. at your current setup that'll still take less than 3 seconds to run.

i wouldn't trust a sample at 1 million either