Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 82853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:40:55+00:00 2026-05-10T21:40:55+00:00

I am writing a Firefox extension. I would like to search the current webpage

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T21:40:56+00:00Added an answer on May 10, 2026 at 9:40 pm

    I’m not sure if it is the fastest but the following worked pretty quickly for me.

    var words = document.body.innerHTML.replace(/<.*?>/g,'').split(/\s+/); var i = words.length; var keywordCounts = {'keyword': 0, 'javascript': 0, 'today': 0}; var keywords = []; var keywordMatcher = ''; var word; for (word in keywordCounts) {     keywords[keywords.length] = word ;     keywordMatcher = keywordMatcher + '(' + word + ')?'; } var regex = new RegExp(keywordMatcher); var j = keywords.length; var matched, keyword; if (i && j) {     do {         i = i - 1;         matched = words[i].match(regex);         if (!matched) continue;         j = keywords.length;         do {             j = j - 1;             if (matched[j + 1]) {                 keyword = keywords[j];                 keywordCounts[keyword] = keywordCounts[keyword] + 1;             }         } while (j);     } while (i); } 

    I’ll definitely grant that from a Big(O) perspective it isn’t the best because as i and j get big it still requires n squared time but I’ve found regular expression processing to generally be pretty fast.

    Basically I’m taking tvanfosson’s idea and expanding on it, but rather than traversing the DOM I’m removing the tags with a regex (the first line) and then splitting the page into individual words. The keyword ‘hash’ is defined on the third line with initial counts (they should all start at zero obviously). From there I a new regular expression is constructed using each keyword as a group so when matched it returns an array of results that has (in my example) [fullMatch,keywordMatch,javascriptMatch,todayMatch]. I’m using decrementing do while loops because they’ve been shown in lots of places to be the fastest looping structure in JavaScript and since it doesn’t matter in what order the words get processed loop speed is really the only consideration.

    I hope this is helpful, if not it was at least a fun exercise. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Firefox extension, and would like to display a password prompt. I
I would like to provide a mechanism for a Firefox4-compatible extension I'm writing, to
I'm writing a Firefox extension and I need to find the ip address of
I'm writing a Firefox extension that's to be used in house. Basically, I need
I am writing a firefox extension that interacts with a JSON server interface. I
I am writing my first FireFox extension and I have some questions. Maybe someone
I'm writing an extension for Firefox, and I need to log some data to
I am writing a FireFox-Extension and want to load Data from Server. But when
I'm writing a Firefox extension that needs to inject a css file into webpages.
I am writing a Firefox extension. I have setup an overlay for chrome://browser/content/browser.xul and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.