Last active
August 24, 2022 18:47
-
-
Save krav4enkodm/63142cbe27e0a00a397d522606abafce to your computer and use it in GitHub Desktop.
Array.include vs Set.has
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 arr = Array.from({ length: 10000 }, (_, i) => i); | |
| const set = new Set(arr); | |
| function checkArr() { | |
| return arr.filter((item) => arr.includes(item)); | |
| } | |
| function checkSet() { | |
| return arr.filter((item) => set.has(item)); | |
| } | |
| function checkPerformance(callback) { | |
| const start = performance.now(); | |
| callback(); | |
| const end = performance.now(); | |
| return end - start; | |
| } | |
| const arrResults = []; | |
| const setResults = []; | |
| for (let i = 0; i < 100; i++) { | |
| arrResults.push(checkPerformance(checkArr)); | |
| setResults.push(checkPerformance(checkSet)); | |
| } | |
| function calcAverage(result) { | |
| return result.reduce((acc, next) => acc + next, 0)/result.length; | |
| } | |
| console.log('arr:', calcAverage(arrResults)); | |
| console.log('set:', calcAverage(setResults)); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
length: 10k
length: 1k
length: 100