I am writing a Firefox extension. I would like to search the current webpage for a set of words, and count how many times each occurs. This activity is only performed when the user asks, but it must still happen reasonably quickly.
I am currently using indexOf on the BODY tag's innerHTML element, but am finding it too slow to run repeatedly in the following manner:
function wordcount(doc, match)
{
var count = 0;
var pos = 0;
for(;;)
{
len=doc.indexOf(match, pos);
if(len == -1)
{
break;
}
pos = len + match.length;
count++;
}
return count;
}
var html = content.document.body.innerHTML.toLowerCase()
for(var i=0; i<keywords.length; i++)
{
var kw = keywords[i];
myDump(kw + ": " + wordcount(html, kw));
}
With 100 keywords, this takes approximately 10 to 20 seconds to run. There is some scope to reduce the number of keywords, but it will still need to run much quicker.
Is there a more obvious way to do this? What is the most efficient method? I have some ideas, but am reluctant to code each up without some idea of the performance I can expect:
- Navigate the DOM rather than using innerHTML. Will this be likely quicker or slower? It would have the benefit of only searching textual content.
- Loop through the document word by word, accumulating a count of each word's occurence simultaneously. With this method I would have to do a bit more work parsing the HTML.
Edit: Turns out that the slowest part was the myDump function writing to the error console. Duh! Nevertheless, there some interesting more efficient alternatives have been presented, which I am intending to use.