Created
January 22, 2019 06:24
-
-
Save cold-logic/72a67ea7fd5e03122001b2a29b2d2680 to your computer and use it in GitHub Desktop.
Performant JS Filter Array Unique
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// benchmarked in milliseconds against an array of 7 million members | |
// safari 12 = 79 | |
// chrome 67 = 115 | |
// node 10 = 97 | |
arrFilter = (arr) => { | |
return arr.filter((elem, pos, array) => { | |
// if the element's index in array doesn't equal pos, it will be added | |
return array.indexOf(elem) == pos | |
}) | |
} | |
arrDupes = (arr) => { | |
// returns just the duplicated elements | |
return arr.filter((elem, pos) => array.indexOf(elem) !== pos) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment