DEV Community

Functional Javascript
Functional Javascript

Posted on

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));

//b. Set-Spread
const getUniq_Set = a => [ 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);


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


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:

Discussion (4)

patarapolw profile image
Pacharapol Withayasakpunt • Edited on

Objects won't work with non-string keys, as well as nasty strings (e.g. __proto__).

functional_js profile image
Functional Javascript Author

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.


const aDups = [{ id: 1 }, { id: 1 }, { id: 1 }, { id: 1}];
const setDups = new Set(aDups)

// output:
// Set(4) { { id: 1 }, { id: 1 }, { id: 1 }, { id: 1 } }

nickfazzpdx profile image
Nicholas Fazzolari

Thank you for sharing the timeInLoop source code. Is the console.time() function where the measurement of time is actually done. I wonder how runtime is calculated in the Console code module.

____marcell profile image
Marcell Cruz

Interesting, why is Set so slow, I would have guessed that it was going to be a lot faster