Break doTest() into sub-functions for profiling
authorAryeh Gregor <AryehGregor+gitcommit@gmail.com>
Mon, 21 Mar 2011 12:06:18 -0600
changeset 19 281b2ec74d47
parent 18 676aa904bd45
child 20 43eb55543101
Break doTest() into sub-functions for profiling

It looks like WebKit does Selection.removeAllRanges() incredibly slowly.
Unfortunately, there's no obvious way to work around it. Not sure
what's making Gecko so slow.
autoimplementation.html
--- a/autoimplementation.html	Mon Mar 21 11:12:28 2011 -0600
+++ b/autoimplementation.html	Mon Mar 21 12:06:18 2011 -0600
@@ -258,19 +258,17 @@
 }
 
 function addTest(command, test) {
-	var value = {
-		bold: null,
-		fontname: "serif",
-		forecolor: "#FF0000",
-		italic: null,
-	}[command];
-	var styleWithCss = {
-		bold: false,
-		fontname: true,
-		forecolor: true,
-		italic: false,
-	}[command];
+	var tr = doSetup(command);
 
+	doInputCell(tr, test);
+	doSpecCell(tr, test, command);
+	doBrowserCell(tr, test, command);
+	doSameCell(tr);
+
+	doTearDown(command);
+}
+
+function doSetup(command) {
 	var div = document.getElementById(command);
 	div.contentEditable = "true";
 	var table = div.querySelector("table");
@@ -284,10 +282,23 @@
 	//	table.insertBefore(tr, table.childNodes[1]);
 	//}
 
+	return tr;
+}
+
+function doInputCell(tr, test) {
 	var inputCell = document.createElement("td");
 	inputCell.innerHTML = test;
 	inputCell.innerHTML = "<div>" + inputCell.innerHTML + "</div><div>" + inputCell.innerHTML.replace(/\&/g, "&amp;").replace(/</g, "&lt;") + "</div>";
 	tr.appendChild(inputCell);
+}
+
+function doSpecCell(tr, test, command) {
+	var value = {
+		bold: null,
+		fontname: "serif",
+		forecolor: "#FF0000",
+		italic: null,
+	}[command];
 
 	var specCell = document.createElement("td");
 	specCell.appendChild(document.createElement("div"));
@@ -308,6 +319,21 @@
 	} catch (e) {
 		specCell.textContent = "Exception: " + e;
 	}
+}
+
+function doBrowserCell(tr, test, command) {
+	var value = {
+		bold: null,
+		fontname: "serif",
+		forecolor: "#FF0000",
+		italic: null,
+	}[command];
+	var styleWithCss = {
+		bold: false,
+		fontname: true,
+		forecolor: true,
+		italic: false,
+	}[command];
 
 	var browserCell = document.createElement("td");
 	browserCell.appendChild(document.createElement("div"));
@@ -327,12 +353,14 @@
 	} catch (e) {
 		browserCell.textContent = "Exception: " + e;
 	}
+}
 
+function doSameCell(tr) {
 	var sameCell = document.createElement("td");
 	// Ad hoc normalization to avoid basically spurious mismatches
-	var normalizedSpecCell = specCell.lastChild.textContent
+	var normalizedSpecCell = tr.childNodes[1].lastChild.textContent
 		.replace(/;? ?"/g, '"');
-	var normalizedBrowserCell = browserCell.lastChild.textContent
+	var normalizedBrowserCell = tr.childNodes[2].lastChild.textContent
 		.replace(/;? ?"/g, '"')
 		.replace(/<(\/?)strong/g, '<$1b')
 		.replace(/<(\/?)em/g, '<$1i')
@@ -345,9 +373,11 @@
 		sameCell.innerHTML = "&#x2717;";
 	}
 	tr.appendChild(sameCell);
+}
 
+function doTearDown(command) {
 	getSelection().removeAllRanges();
-	div.contentEditable = "inherit";
+	document.getElementById(command).contentEditable = "inherit";
 }
 
 function parseBrackets(node) {