Code Snippet Series: Get Unique Values from Array

Functional Javascript - Aug 4 '20 - - Dev Community

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 => [...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

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player