Shorter code isn't always better code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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