[PATCH 2 of 4 V2] hgweb: optimize process_dates function

Alexander Plavin alexander at plav.in
Sat Aug 17 17:35:18 CDT 2013


# HG changeset patch
# User Alexander Plavin <alexander at plav.in>
# Date 1374682432 -14400
#      Wed Jul 24 20:13:52 2013 +0400
# Node ID 122061a7a549d6e3faa84a0657699b5ed705f413
# Parent  6106b5b83c38ce74e67dae5a11e111b46a5b4842
hgweb: optimize process_dates function

This function looped over every node due to getElementsByTagName('*'), instead
of using selectors. In this patch we use querySelectorAll('.age') and process
only these nodes, which is much faster and also doesn't require extra condition.

Browser compatibility isn't sacrificed: IE 8+, FF 3.5+, Opera 10+.

diff -r 6106b5b83c38 -r 122061a7a549 mercurial/templates/static/mercurial.js
--- a/mercurial/templates/static/mercurial.js	Wed Jul 24 18:53:55 2013 +0400
+++ b/mercurial/templates/static/mercurial.js	Wed Jul 24 20:13:52 2013 +0400
@@ -245,21 +245,18 @@
 		}
 	}
 
-    var nodes = document.getElementsByTagName('*');
-    var ageclass = new RegExp('\\bage\\b');
+    var nodes = document.querySelectorAll('.age');
     var dateclass = new RegExp('\\bdate\\b');
     for (var i=0; i<nodes.length; ++i){
         var node = nodes[i];
         var classes = node.className;
-        if (ageclass.test(classes)){
-            var agevalue = age(node.textContent);
-            if (dateclass.test(classes)){
-                // We want both: date + (age)
-                node.textContent += ' ('+agevalue+')';
-            } else {
-                node.title = node.textContent;
-                node.textContent = agevalue;
-            }
+        var agevalue = age(node.textContent);
+        if (dateclass.test(classes)){
+            // We want both: date + (age)
+            node.textContent += ' ('+agevalue+')';
+        } else {
+            node.title = node.textContent;
+            node.textContent = agevalue;
         }
     }
 }


More information about the Mercurial-devel mailing list