find unique (v4)

Revision 4 of this benchmark created on


Setup

const SIZE = 100000;
const randomIntArray = _.times(SIZE, () => _.random(1, 1000)); // Adjust range as needed

Test runner

Ready to run.

Testing in
TestOps/sec
青蛙 code
function getUniqueNumber (items) {
  let arr = [];
  items.forEach((i) => {
    if (arr.includes(i)) return;
    arr.push(i);
  });
  return arr;
}

getUniqueNumber(randomIntArray )
ready
大猩猩
function getUniqueNumber (items) {
  const uniqSet = new Set();

  items.forEach((num) => {
    uniqSet.add(num);
  });
  
  return Array.from(uniqSet);
}
getUniqueNumber(randomIntArray )
ready
大猩猩2
function getUniqueNumber(items) {
  const collection = {};

  items.forEach((num) => {
    collection[num] = true;
  });

  return Object.keys(collection);
}

getUniqueNumber(randomIntArray);
ready
青蛙 code 2
function getUniqueNumber (items) {
  let arr = [];
  const collection = {};
  
  items.forEach((num) => {
  	// if exists, continue
  	if (collection[num]) return;
    
    // else
    arr.push(num);
    collection[num] = true
  });
  
  return arr;
}

getUniqueNumber(randomIntArray )
ready

Revisions

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