I have an array that consists of several words and I am finding a particular word. My current solution, which is below, works perfectly fine for small array. But if this array contains lets say 10,000 words then my solution would require a lot memory and CPU resources as it is not efficient enough. How can I make my code better in terms of performance and should take less resources for a large array in JavaScript?
var words = ['apple', 'orange', 'ananas', 'banana', 'mango', 'lemon', 'tomato'];
function search (term) {
for (var i = 0, len = words.length; i < len; i++) {
if (words[i] === term) {
console.log(words[i] + ' is found at ' + i);
}
}
}
search('tomato');