Patchwork [2,of,4,V2] hgweb: optimize process_dates function

login
register
mail settings
Submitter Alexander Plavin
Date Sept. 29, 2013, 5:15 p.m.
Message ID <306b4b9f13c96dbe7f69.1380474934@debian-alexander.dolgopa>
Download mbox | patch
Permalink /patch/2663/
State Accepted
Commit 14fddba036f89cf4935a80e392342e3d306f0aa2
Headers show

Comments

Alexander Plavin - Sept. 29, 2013, 5:15 p.m.
# HG changeset patch
# User Alexander Plavin <alexander@plav.in>
# Date 1378459858 -14400
#      Fri Sep 06 13:30:58 2013 +0400
# Node ID 306b4b9f13c96dbe7f69f3cfb67a0315ae71ff36
# Parent  ef97e7b67fe60c99181dc9153a15451077ece88d
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+.

Patch

diff -r ef97e7b67fe6 -r 306b4b9f13c9 mercurial/templates/static/mercurial.js
--- a/mercurial/templates/static/mercurial.js	Fri Sep 06 13:30:58 2013 +0400
+++ b/mercurial/templates/static/mercurial.js	Fri Sep 06 13:30:58 2013 +0400
@@ -253,21 +253,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;
 		}
 	}
 }