Skip to content
Advertisement

JS – Is there a more efficient way to compare values in an array to a target search term

I am aiming to search an array of objects for one that’s title is similar or matches a search term exactly. The problem is I would like to prioritise exact matches over matches that only contain the string.

The current code does this by looping multiple times, each time with a different condition, and returns the object if it matches.

JavaScript

Is there a way to do this more efficiently, like in one loop – or is there a better way to search strings?

I have looked into using the Levenshtein algorithm however this does not work for searching “Comp” and getting the Item with title “Completely Different Title”, as a lot more is different between “Comp” and “Completely Different Title” than there is between “Comp” and “term” – Is there a way to incorporate the same idea into this search?

Advertisement

Answer

If you’re looking for efficiency, the only improvement I can think of that would reduce processing would be to lowercase the strings in advance, instead of lowercasing each value inside each loop. Though, it’d probably be a very marginal improvement and be unnoticeable in most cases.

JavaScript

The logic you want to implement fundamentally requires a loop over all elements looking for one condition, followed by another loop over all elements looking for another, etc. So there’s no real way to improve the computational complexity over your current implementation.

You could combine some or all of the conditions with a regular expression, but that would break the priority sequence of match types to be returned.

If you want to make the code shorter and easier to maintain, that’s easy enough – you could use an array of callbacks that get called for every item in order:

JavaScript

Is there a way to incorporate the same idea into this search?

Checking the Levenshtein distance would be a bit different. Instead of looping over items and returning one when it matches, you’d need to loop over all items unconditionally and return the best match after the loop finishes.

JavaScript

You’d do this at least instead of the .includes check at the end. Depending on what logic you want, you might also remove the startsWith and endsWith checks in exchange too.

Advertisement