de-deupe

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Method 1: Using the filter method and the index of the first occurrence
const array = [1, 2, 3, 4, 1, 2, 3];
const uniqueArray = array.filter((value, index, self) => {
  return self.indexOf(value) === index;
});
console.log(uniqueArray); // [1, 2, 3, 4]
ready
Method 2: Use of the spread operator (…) and the Set object
const array = [1, 2, 3, 4, 1, 2, 3];
const uniqueArray = [...new Set(array)];
console.log(uniqueArray); // [1, 2, 3, 4]
ready
Method 3: Using the reduce method
const array = [1, 2, 3, 4, 1, 2, 3];
const uniqueArray = array.reduce((accumulator, currentValue) => {
  if (!accumulator.includes(currentValue)) {
    accumulator.push(currentValue);
  }
  return accumulator;
}, []);
console.log(uniqueArray); // [1, 2, 3, 4]
ready

Revisions

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