Recursive vs non-recursive read-time estimation
HTML
<script src="https://raw.github.com/bestiejs/benchmark.js/v1.0.0/benchmark.js"></script>
<div id="bodyContent">
<!-- tagline -->
<div id="siteSub">From Wikipedia, the free encyclopedia</div>
<!-- /tagline -->
<!-- subtitle -->
<div id="contentSub"></div>
<!-- /subtitle -->
<!-- jumpto -->
<div id="jump-to-nav" class="mw-jump">
Jump to: <a href="#mw-navigation">navigation</a>, <a href="#p-search">search</a>
</div>
<!-- /jumpto -->
<!-- bodycontent -->
<div id="mw-content-text" lang="en" dir="ltr" class="mw-content-ltr"><div class="dablink">For other uses, see <a href="/wiki/Recursion_(disambiguation)" title="Recursion (disambiguation)">Recursion (disambiguation)</a>.</div>
<table class="metadata plainlinks ambox ambox-content ambox-Refimprove" style="">
<tbody><tr>
<td class="mbox-image">
<div style="width: 52px;"><img alt="" src="//upload.wikimedia.org/wikipedia/en/thumb/9/99/Question_book-new.svg/50px-Question_book-new.svg.png" width="50" height="39" srcset="//upload.wikimedia.org/wikipedia/en/thumb/9/99/Question_book-new.svg/75px-Question_book-new.svg.png 1.5x, //upload.wikimedia.org/wikipedia/en/thumb/9/99/Question_book-new.svg/100px-Question_book-new.svg.png 2x"></div>
</td>
<td class="mbox-text" style=""><span class="mbox-text-span">This article <b>needs additional citations for verification</b>. <span class="hide-when-compact">Please help <a class="external text" href="//en.wikipedia.org/w/index.php?title=Recursion&action=edit">improve this article</a> by <a href="/wiki/Help:Introduction_to_referencing/1" title="Help:Introduction to referencing/1">adding citations to reliable sources</a>. Unsourced material may be <a href="/wiki/Template:Citation_needed" title="Template:Citation needed">challenged</a> and <a href="/wiki/Wikipedia:Verifiability#Burden_of_evidence" title="Wikipedia:Verifiability">removed</a>.</span> <small><i>(June 2012)</i></small>...
JavaScript
var suite = new Benchmark.Suite;
// add tests
suite.add('Recursive', function() {
(function () {
function getTextNodesIn(element) {
var wordcount = 0,
whitespace = /^\s*$/;
function getTextNode(node) {
// type 3 is a textnode
if (node.nodeType == 3) {
// We skip text nodes that are only whitespace
if (!whitespace.test(node.nodeValue)) {
wordcount += node.nodeValue.split(" ").length;
}
// this isn't a text node, so test each childnode
} else {
for (var i = 0, len = node.childNodes.length; i < len; ++i) {
getTextNode(node.childNodes[i]);
}
}
}
getTextNode(element);
return wordcount;
}
console.log("Estimated reading time: " + (getTextNodesIn(document.getElementById('bodyContent')) / 250) + " minutes")
}());
})
.add('Non-recursive', function() {
(function () {
function getTextNodesIn(element) {
var wordcount = 0,
whitespace = /^\s*$/,
nodelist = [element];
while (nodelist.length > 0) {
var node = nodelist.pop();
if (node.nodeType == 3) {
// We skip text nodes that are only whitespace
if (!whitespace.test(node.nodeValue)) {
wordcount += node.nodeValue.split(" ").length;
}
// this isn't a text node, so test each childnode
} else {
for (var i = 0, len = node.childNodes.length; i < len; ++i) {
nodelist.push(node.childNodes[i]);
}
}
}
return wordcount;
}
console.log("Estimated reading time: " + (getTextNodesIn(document.getElementById('bodyContent')) / 250) + " minutes");
}());
})
// add listeners
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function()...