Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Shorter code isn't always better code.
const bigArr = new Array(1000000).fill(1).map((_, i) => i);
bigArr[100] = 5;
const allUnique = arr => {
const all = new Set();
for (let el of arr) {
if (all.has(el)) {
return false;
}
all.add(el);
}
return true;
}
const allUnique2 = arr => arr.length === [...new Set(arr)].length;
console.time("first");
allUnique(bigArr);
console.timeEnd("first");
console.time("second");
allUnique2(bigArr);
console.timeEnd("second");
// VM969:22 first: 0.109130859375ms
// VM969:26 second: 138.579833984375ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment