Skip to content

Instantly share code, notes, and snippets.

@mloureiro
Last active February 28, 2018 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mloureiro/6c6cec8a46e782f8106aba479d811609 to your computer and use it in GitHub Desktop.
Save mloureiro/6c6cec8a46e782f8106aba479d811609 to your computer and use it in GitHub Desktop.
Array deduplication using Object or Array.includes() or Array.indexOf()
function clearDuplicatesUsingArrayIncludes(array) {
const existing = [];
return array.filter((el) => {
if (existing.includes(el)) {
return false;
}
existing.push(el);
return true;
});
}
function clearDuplicatesUsingArrayIndexOf(array) {
const existing = [];
return array.filter((el) => {
if (existing.indexOf(el) !== -1) {
return false;
}
existing.push(el);
return true;
});
}
function clearDuplicatesUsingObject(array) {
const existing = {};
return array.filter((el) => {
if (existing[el] === true) {
return false;
}
existing[el] = true;
return true;
});
}
// using the approach in https://codereview.stackexchange.com/a/60137
function unique() {
const sorted = this;
sorted.sort();
return sorted.filter(function(value, index, arr){
if(index < 1)
return true;
else
return value != arr[index-1];
});
}
function testUnique(array) {
return unique.bind(array)();
}
function test(array, duplicateCallback, type) {
const start = Date.now();
const result = duplicateCallback(array);
const end = Date.now();
console.log(type, `${(end - start)/1000}s`, result.length);
}
// just random integer with a lot of duplicates in all range of values
const list = (new Array(1000000))
.fill(null)
.map((_, i) => {
const x = Math.floor(i/5);
if (x % 3 === 0) return x;
return i % 3 === 0 ? i - 1 : i;
});
test(list, clearDuplicatesUsingArrayIncludes, 'ArrayIncludes');
test(list, clearDuplicatesUsingArrayIndexOf, 'ArrayIndexOf');
test(list, clearDuplicatesUsingObject, 'Object');
test(list, testUnique, 'Unique');
// Result:
// ArrayIncludes 533.701s 533332
// ArrayIndexOf 675.677s 533332
// Object 0.143s 533332
// Unique 1.077s 533332
// The test was run in my local machine with other stuff running so it is
// not 100% accurate although the time difference is so big that the
// environment is irrelevant
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment