const SIZE_OF_ARRAY = 100000; // 10, 1000, 100000, 1000000
const anArray = Array.from(Array(SIZE_OF_ARRAY + 1).keys());
anArray.shift(); // Array(10) [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
const SEARCH_VALUE = Math.floor(SIZE_OF_ARRAY / 2); // i.e. 5
let foundIt = false;
const callback = (x) => x === SEARCH_VALUE
foundIt = anArray.includes(SEARCH_VALUE); // true
foundIt = anArray.filter(callback).length > 0; // true
foundIt = anArray.some(callback); // true
const length = anArray.length // cache the array's length to improve performance of for loop
for (let index = 0; index < length; index++) {
if (anArray[index] === SEARCH_VALUE) {
foundIt = true;
break;
}
}
// Reference on caching the array's length: https://medium.com/@tiggr/well-youve-got-quite-a-mess-there-haven-t-you-5ae385f22f67
foundIt = anArray.find(callback) === SEARCH_VALUE; // true
// console.log(foundIt) // true
// Author's note: according to a GitHub issue, checking the value of the result using console.log() could make a difference here.
// See https://github.com/dg92/Performance-Analysis-JS/issues/14
// I did not find that to be true in my testing, but it's worth mentioning here.