I have an array (1000 pieces) of brands, where I would like to provide a fast search. So when I start typing ie: "m" then I should get "mammut" and "millet". The array is sorted. So does anybody know a fast solution which doesn't need to loop through the whole array? Best in javascript. Thanks
var brands = new Array("arcterix", "mammut", "millet", "ortovox", ... )
function search(brands, substring){
// will return array of founded brands which begins on substring
}
Try this
var brands = ["arcterix", "mammut", "millet", "ortovox"]
function search(brands, substring){
return brands.filter( i => i.startsWith(substring) )
}
console.log(search(brands, 'm')) // ['mammut', 'millet']
This is very fast. It's almost impossible to do things faster, everything is optimized for you.