Native Unique vs ember uniqBy with concat

Benchmark created on


Setup

const repeat = (arr, n) => Array(n).fill(arr).flat();

const list = [1,2,3,4,5,6,7,9,10, ...repeat(11, 500)];

const list2 = [10,20,30,40,50,60,70,90,100, ...repeat(110, 500)];

const identityFunction = (item) => item;
function uniqBy(
  array,
  keyOrFunc = identityFunction
) {

  let ret = [];
  let seen = new Set();
  let getter = typeof keyOrFunc === 'function' ? keyOrFunc : (item) => item[keyOrFunc];

  array.forEach((item) => {
    let val = getter(item);
    if (!seen.has(val)) {
      seen.add(val);
      ret.push(item);
    }
  });

  return ret;
}

Test runner

Ready to run.

Testing in
TestOps/sec
Merge array with set
const newArr = Array.from(new Set(list.concat(list2)));
ready
Merge array ember
const newArr = uniqBy(list.concat(list2));
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.