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.
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.
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.
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.
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.
(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.
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.
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
20
u/slowthedataleak May 14 '21
Does this perform better than filter()?