I’m trying to find a fast way to search for partial strings in array elements and put them into a new array if the input value is greater than 2 characters.
Ultimately, this array will be used to create a list as results are found, a local data search.
This works okay but the arrays can be quite large (thousands of entries) and was looking for the best way to approach this that won’t end up a performance problem.
<html>
<body>
<input id="test">
</body>
<script>
let test = ['Fruits: Apples', 'Fruits: Oranges', 'Fruits: Banannas', 'Fruits: Lemons', 'Vegetables: Corn', 'Vegetables: Potatoes']
let found = []
document.querySelector('#test').addEventListener('keyup', e => {
let value = e.currentTarget.value
if (value.length > 2) {
let index = test.findIndex(i => i.includes(value))
test.forEach(v => {
if(v.includes(value) && !found.includes(v))
found.push(v)
})
} else {
found = []
}
console.log(found)
})
</script>
</html>
2
Answers
First of all, you should examine, if it is really a performance problem, because as Don Knuth once said "Premature optimization is the root of all evil".
Unless you create a fulltext index over your
test
array, there is no other way than looking at each element in the values range if it contains your search teams.But you could use a
Set
instead of an array for yourfound
collection. This way you can just doThe duplicate check on a
Set
is much faster than in an array, and will be done automatically by theadd()
function.As a further optimization you could consider restricting the collection of values you need to search, when your add additional characters to your searchterm. Ie when your searchterm first was
Fru
and now isFruits
, matches can only come from your previousfound
set, because a term, that doesn’t containFru
can’t containFruits
. Not sure if this suggestion meets your usecase, though … And it will of course only work for follow up searches under certain conditions.Array::filter()
just filter the array.A more complex and potentially more performant option could be to build a suffix array and binary search it: