Remove repeated data (v2)

Revision 2 of this benchmark created on


Setup

const generateRandomNumbers = (size, max) => Array.from({ length: size }, () => Math.floor(Math.random() * (max + 1)));

const data = generateRandomNumbers(10000, 9);


Test runner

Ready to run.

Testing in
TestOps/sec
for loop
const removeRepeated = (data) => {
  const result = [];
  for (let i = 0, len = data.length; i < len; i++) {
    if (i === 0 || data[i] !== data[i - 1]) {
      result.push(data[i]);
    }
  }
  return result;
};

const result = removeRepeated(data);
ready
Filter
const result = data.filter((item, idx, arr) => !idx || item !== arr[idx - 1]);
ready
Author's code
const result = [data[0]];
let lastElement = data[0];
for (let i = 1, n = data.length; i < n; ++i) {
  if (data[i] !== lastElement) {
    lastElement = data[i];
    result.push(lastElement);
  }
}
ready

Revisions

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