Loop Text Nodes (v2)

Revision 2 of this benchmark created on


Description

Fastest way to loop through all DOM text nodes

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<h1><strong><em title="JavaScript">js</em>Perf</strong> — JavaScript performance playground</h1>
<h2>What is jsPerf?</h2>
<p>jsPerf aims to provide an easy way to create and share <a href="/browse" title="View some examples by browsing the jsPerf test cases">test cases</a>, comparing the performance of different JavaScript snippets by running benchmarks. For more information, see <a href="/faq" title="Frequently asked questions">the FAQ</a>.</p>
<h2>Create a test case</h2>
<h1><strong><em title="JavaScript">js</em>Perf</strong> — JavaScript performance playground</h1>
<h2>What is jsPerf?</h2>
<p>jsPerf aims to provide an easy way to create and share <a href="/browse" title="View some examples by browsing the jsPerf test cases">test cases</a>, comparing the performance of different JavaScript snippets by running benchmarks. For more information, see <a href="/faq" title="Frequently asked questions">the FAQ</a>.</p>
<h2>Create a test case</h2>
<h1><strong><em title="JavaScript">js</em>Perf</strong> — JavaScript performance playground</h1>
<h2>What is jsPerf?</h2>
<p>jsPerf aims to provide an easy way to create and share <a href="/browse" title="View some examples by browsing the jsPerf test cases">test cases</a>, comparing the performance of different JavaScript snippets by running benchmarks. For more information, see <a href="/faq" title="Frequently asked questions">the FAQ</a>.</p>
<h2>Create a test case</h2>

Test runner

Ready to run.

Testing in
TestOps/sec
a
function LoopTextNodes(node)
{
        if (node.nodeType == 3)
        {
                // textnode
        }
        else if (node.hasChildNodes())
        {
                for (var i=0;i < node.childNodes.length;i++)
                {
                        LoopTextNodes(node.childNodes[i]);
                }
        }
}
LoopTextNodes(document.body);
ready
b
function LoopTextNodes(node)
{
        if (node.nodeType == 3)
        {
                // textnode
        }
        else if (node.hasChildNodes())
        {
                var i = node.childNodes.length;

                while(i > 0)
                {
                        LoopTextNodes(node.childNodes[--i]);
                }
        }
}
LoopTextNodes(document.body);
ready
c
$('*')
        .contents()
        .filter(function() {
                return this.nodeType == 3;
        })
        .each(function(){
                // textnode
        });
 
ready
d
function LoopTextNodes(node)
{
        if(node)
        {
                node = node.firstChild;
                while(node != null)
                {
                        if(node.nodeType== 3)
                        {
                                // textnode
                        }
                        else
                                LoopTextNodes(node);

                        node = node.nextSibling;
                }
        }
}
LoopTextNodes(document.body);
ready
e
function LoopTextNodes()
{
        var     walker = document.createTreeWalker(
                        document.body, 
                        NodeFilter.SHOW_TEXT, 
                        null, 
                        false
                ),
                node,
                textNodes = [];

        while(node = walker.nextNode())
        {
                //textnode
        }
}
LoopTextNodes();
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.