## DEV Community is a community of 623,427 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Code Snippet Series: Get Unique Values from Array

Here are three ways to retrieve the unique set of primitive values from an array....

``````//a. Set-Array.from
const getUniq_ArrayFrom = a => Array.from(new Set(a));

const getUniq_Set = a => [...new Set(a)];

//c. good ol' Loop
const getUniq_Loop = a => {
const o = {};
for (let i = 0; i < a.length; i++) {
o[a[i]] = true;
}
return Object.keys(o);
};

//@perfTests

timeInLoop("getUniq_ArrayFrom", 1, () => getUniq_ArrayFrom(aNums));
// getUniq_ArrayFrom: 1e+0: 513.777ms

timeInLoop("getUniq_Set", 1, () => getUniq_Set(aNums));
// getUniq_Set: 1e+0: 521.112ms

timeInLoop("getUniq_Loop", 1, () => getUniq_Loop(aNums));
// getUniq_Loop: 1e+0: 44.433ms
``````

I think we have a clear winner here. 🏆
The loop wins by over 10X

# Notes:

using an array of 10 million numbers between 1 and 100 (high duplication), the Loop idiom is 10X faster...

``````const aNums = genRandNums(1, 100, 1e7);
``````

using an array of 10 million numbers between 1 and a million (low duplication), the Loop idiom is only 2X faster...

``````const aNums = genRandNums(1, 1e6, 1e7);
``````

# TimeInLoop Source Code:

https://gist.github.com/funfunction/91b5876a5f562e1e352aed0fcabc3858

## Discussion (4)  Functional Javascript

Correct, these funcs work with the primitives (string and number).

Ref types (comparing objects and arrays) require custom logic, even if working with Set and Map.

eg...

``````const aDups = [{ id: 1 }, { id: 1 }, { id: 1 }, { id: 1}];