Made a function that searches a specific string in a very very large array of strings using Levenshtein Distance. But it's too slow. Takes around 6000-10000ms to complete. How can I make it fast? I expected the function to end in less than 1000ms. Is it possible by using worker threads, cluster or anything else to run the loop in parallel so that I'll get the result in less than a second?
const leven = require('leven');
const veryLargeArray = ['foo bar foo', 'ajskdnasjkd', 'fooo fooo bar fooo', 'bar fooo', /*This array in my actual code has more than 900K strings*/];
function search(str, arr) {
const results = [];
for (const item of arr) {
results.push({ item, distance: leven(str, arr[i]) });
}
return results;
}
search('hello', [...veryLargeArray]);