Skip to content

Instantly share code, notes, and snippets.

@anthonybrown
Created September 2, 2019 11:12
Show Gist options
  • Save anthonybrown/de109f49abb499e43744c98fa8e4d92d to your computer and use it in GitHub Desktop.
Save anthonybrown/de109f49abb499e43744c98fa8e4d92d to your computer and use it in GitHub Desktop.
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