Skip to content

Instantly share code, notes, and snippets.

@cold-logic
Created January 22, 2019 06:24
Show Gist options
  • Save cold-logic/72a67ea7fd5e03122001b2a29b2d2680 to your computer and use it in GitHub Desktop.
Save cold-logic/72a67ea7fd5e03122001b2a29b2d2680 to your computer and use it in GitHub Desktop.
Performant JS Filter Array Unique
// 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