Last active
February 17, 2025 09:29
-
-
Save leipreachan/0af29a8f5c25eab8dd28fd741966d39b to your computer and use it in GitHub Desktop.
different solutions to check for duplicates
This file contains hidden or 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 hasDuplicateOneline = (array) => | |
new Set(array).size != array.length; | |
const hasDuplicateAlternative = (arr) => | |
arr.some((value, index, origin) => origin.indexOf(value) !== index); | |
let z = []; | |
for (let i = 0; i < 10_000; i++) { | |
z.push(i); | |
} | |
// z.push(1); | |
const testFunctions = [hasDuplicateOneline, hasDuplicateAlternative]; | |
let start, stop, avg, execTime = {}; | |
for (let index in testFunctions) { | |
const func = testFunctions[index]; | |
execTime[func.name] = []; | |
for (let j=0; j<1000; j++) { | |
performance.mark('start'); | |
testFunctions[index](z); | |
performance.mark('stop'); | |
performance.measure('executionTime', 'start', 'stop'); | |
execTime[func.name].push(performance.getEntriesByName('executionTime')[0].duration); | |
} | |
performance.clearMarks(); | |
performance.clearMeasures(); | |
} | |
console.log(`${z.length} elements:`); | |
for (let index in testFunctions) { | |
const func = testFunctions[index]; | |
avg = execTime[func.name].reduce((prev, cur) => {return prev + cur}, 0) / execTime[func.name].length; | |
console.log(`${func.name} ${execTime[func.name].length} runs avg: ${avg}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
well pointed, I updated the code. Yet the difference is still noticeable