All of JS's list processing functions are pretty inefficient, since at the bare minimum each one creates and copies to a new array (as opposed to, say, Rust iterators). You use them when elegance is more important than performance; N^2 is fine when N is eight.
That's always such a dangerous proposition though. Sure _you_ remember it's only fine when N is 8, but then the next guy comes along, or the input constraints change, or future you forgets because it _is_ fairly elegant looking.
I try not to leave grenades laying around too often, myself.
The other advantage of the filter one is it can be stuck in the middle of a bunch of other list operations - map(), sort(), slice(), other filters(). Also, you can use findIndex() instead of indexOf() to match any arbitrary predicate, instead of exact equivalence.
Comments (380) 116 threads
Show all comments
Showing thread 62 of 116
I try not to leave grenades laying around too often, myself.
The one I posted just blew my mind a little the first time I saw it, so I love to share it. I guess I never really considered spreading a set.