Ask coding questions

← Back to all posts
How to efficiently search an array for keywords in Node.js
Dunce (71)

Hello, I'm working on making a search engine, I already made a web crawler which stores the URLs it finds in Repl DB.

Now I'm wondering how I would efficiently search the database. The database's keys are the raw text of the webpages, the values are the URLs of the webpages.

Database.prototype.list() method returns an array of all the database's keys, so I'm thinking I'll search the array for keywords, but I'm not sure what the best method of doing this is, the best I could find is Array.prototype.filter().

Answered by ch1ck3n (2359) [earned 5 cycles]
View Answer
ch1ck3n (2359)

var i;arr.forEach(function(item, index){if (item.contains(myString) i += 1)})

Dunce (71)

@ch1ck3n Thanks, although this is going to be searching through potontially large amounts of data, so this seems like it would be slow. Is there a better method?

ch1ck3n (2359)

@Dunce If you're going to filter items you're going to go through every item in an array...

Dunce (71)

@ch1ck3n Yeah, but I figured some smart person would of come up with a super efficient algorithm or something. I mean it seems like if for example Google searched this way it would be very slow.

IDK, maybe I'm wrong, thanks for the help.

ch1ck3n (2359)

@Dunce you can just cache results for like a day or two and just reload the search results after

Coder100 (18903)

@ch1ck3n yoo that's inefficient
use reduce

let i = arr.reduce((arr, current) => arr + current.includes(str), 0);