Skip to content

Instantly share code, notes, and snippets.

@sgatu
Created June 12, 2023 09:38
Show Gist options
  • Select an option

  • Save sgatu/306f3f1700122186ddaf4857acb5d9a7 to your computer and use it in GitHub Desktop.

Select an option

Save sgatu/306f3f1700122186ddaf4857acb5d9a7 to your computer and use it in GitHub Desktop.
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