Created
June 12, 2023 09:38
-
-
Save sgatu/306f3f1700122186ddaf4857acb5d9a7 to your computer and use it in GitHub Desktop.
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
| function shuffle(array) { | |
| let currentIndex = array.length, randomIndex; | |
| // While there remain elements to shuffle. | |
| while (currentIndex != 0) { | |
| // Pick a remaining element. | |
| randomIndex = Math.floor(Math.random() * currentIndex); | |
| currentIndex--; | |
| // And swap it with the current element. | |
| [array[currentIndex], array[randomIndex]] = [ | |
| array[randomIndex], array[currentIndex]]; | |
| } | |
| return array; | |
| } | |
| let arr = []; | |
| let max = 1000000; | |
| for(let i = 0; i < max; i++){ | |
| arr.push(i); | |
| } | |
| shuffle(arr); | |
| let set = new Set(arr); | |
| console.time("arr.includes"); | |
| for(let i = 0; i < max;i++){ | |
| arr.indexOf(i); | |
| } | |
| console.timeEnd("arr.includes"); | |
| console.time("set.has"); | |
| for(let i = 0; i < max;i++){ | |
| set.has(i); | |
| } | |
| console.timeEnd("set.has"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment