As an exercise for myself to practivepractice my JavaScript "skills" I'm trying to write (let's reinvent) a client-side filter. It should be able to filter "content blocks" as the clientuser types.
Wit aBy "content block", I mean a list of DomElements that each contain atleastat least one text node (but- it is possible that they contain more, and even a different amount of text nodes, nested inside other nodes, ..etc.)
Obviously there areI've thought of 2 approaches:
on init:On page initialization, scan all nodes and store all the text in some kind of Map or a tree or whatever.
Simply iterate over every item and check wetherwhether it has the string to search/filter for. One could add performance here by caching, only filtering the current remaining items if text is added, ..etc.
Obviously, if the numernumber of nodes is really big, option 1) will take a while to build the 'index' but it will obviousl perform faster once it is built. Option 2) however will be available writeright on pageloadpage load since no initinitialization is performed. But ofcourseof course it will take longer to search...
So my question, is: what is the best approach here? And how would one implement 'caching' and/or 'index' and/or ... ?