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

  • Home
  • SEARCH
  • 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 7589255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:06:53+00:00 2026-05-30T20:06:53+00:00

I have been trying different methods of doing this, however they are all quite

  • 0

I have been trying different methods of doing this, however they are all quite slow.

noGlobalTags is an array containing three tags.

Method 1:

var textNodeWalker = document.createTreeWalker(node, NodeFilter.SHOW_TEXT, function(node){return (noGlobalTags.indexOf(node.tagName)==-1)? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;}, false);
var cn;
while(cn = textNodeWalker.nextNode()){
  textNodeEmoteParser(cn);
}
return;

Method 2:

function getTextNodes(node) {
    if(node.nodeType == 3) {
        textNodeEmoteParser(node);
    } else {
        for(var i = 0, len = node.childNodes.length; i < len; ++i) {
            if(noGlobalTags.indexOf(node.childNodes[i].tagName)!=-1) {
                continue;
            }
            getTextNodes(node.childNodes[i]);
        }
    }
}

getTextNodes(node);

I had expected method 1 to be the quickest, however it took double as long as the second method. Am I doing something stupid here? If not, can I make it faster than the second method?

  • 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. Editorial Team
    Editorial Team
    2026-05-30T20:06:55+00:00Added an answer on May 30, 2026 at 8:06 pm

    In javascript, inline code (as in the 2nd example), is nearly always faster than using callbacks (as in the 1st example). Javascript has a meaningful amount of overhead for a function call.

    So, the generic treeWalker method with callbacks isn’t as likely to every be as fast as a direct coded method in your 2nd example.

    One finds the same thing is true when comparing a for loop direct iteration of an array to a .forEach() iteration of the same array for the same reaons. Callback functions are slow.

    The code with callback functions can often be more generically useful and sometimes much shorter, but not necessarily faster to execute.

    This shouldn’t be surprising if you think about what’s happening in javascript. To call a function, it has to do a lookup of the name, then it has to create a new function context, pass the parameters to the function, create any local variables in that function, execute the code in that function, clean up all of the above when done, return from that function and continuing execution after the function call. The inline code version skips a lot of these steps.

    FYI, you can speed both of them up if you put the tags in an object instead of an array:

    var noGlobalTags = {"LI": true, "UL": true};
    

    Then, you can check if a given tag is in that list with this type of code:

    noGlobalTags[node.tagName]                    // your first example
    

    or

    if (noGlobalTags[node.childNodes[i].tagName])   // your second example
    

    which should be significantly faster than the array search you are currently using.

    Here’s a non-recursive version that also uses the faster noGlobalTags lookup:

    function getTextNodes(top) {
        var queue = [], index, item, node;
    
        // put the first node into the work queue
        queue.unshift({nodes: [top], pos: 0});
    
        // keep going while more work to go
        while (queue.length) {
            item = queue[0];
            index = item.pos++;
            // if there are more items to go, process the next one
            if (index < item.nodes.length) {
                node = item.nodes[index];
                if (node.nodeType == 3) {
                    // if it's a text node, call the parser
                    textNodeEmoteParser(node);
                } else {
                    // not a text node, get it's children
                    // and put them at the start of the array 
                    // to be processed next
                    if (node.childNodes.length && !noGlobalTags[node.tagName]) {
                        queue.unshift({nodes: node.childNodes, pos: 0});
                    }
                }
            } else {
                // no more of these children to process
                // remove them from the queue
                queue.shift();
            }
        }
    }
    

    EDIT: nearly 100x faster. I found a different method that actually just walks the tree directly that is a lot faster:

    function customIterativeTreeWalker(root) {
        var node = root.firstChild;
        while(node != null) {
            if(node.nodeType == 3) {
                textNodeEmoteParser(node);
            }
            if (node.hasChildNodes()) {
                // if it has a tagName and the tabName is in our table
                // skip going into the tag
                if (node.tagName && noGlobalTags[node.tagName]) {
                    node = node.nextSibling;
                } else {
                    // go down into the children
                    node = node.firstChild;
                }
            } else {
                while (node.nextSibling == null) {
                    node = node.parentNode;
                    if (node == root) {
                        return;
                    }
                }
                node = node.nextSibling;
            }
        }
    }
    

    JSPerf here: http://jsperf.com/get-text-nodes-non-recursive/3

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

Sidebar

Related Questions

I have been trying to parse the bottom table on this site using different
I've been posting different versions of this code all day (sorry!). I'm now trying
For the past 12 hours I've been trying all different methods I can think
I have been trying different things and still have been able to get exactly
I have been trying to find the different between MVC and 3-tier architecture in
I have been trying to generate report as per age differences of different months
I am trying to learn assembly my self, and I have been reading different
Good Day, I have been trying various methods both found on here and in
I've been trying to implement this for a long time and I have gotten
I have been Googling and toying around with different methods and I am still

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.