changeset 1813:4c61bb7acb9b

Minor js optimizations. * flatten() now uses accumulator and join. * reuse regexps instead of allocating new each time
author Vladimir Shabanov <vshabanoff@gmail.com>
date Thu, 30 Aug 2012 17:58:37 +0400
parents ea5f920ffa35
children 2d9f831d45c9
files lib/js/urweb.js
diffstat 1 files changed, 19 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/lib/js/urweb.js	Thu Aug 30 17:04:30 2012 +0400
+++ b/lib/js/urweb.js	Thu Aug 30 17:58:37 2012 +0400
@@ -595,15 +595,22 @@
     return closures[n];
 }
 
-function flatten(cls, tr) {
-    if (tr.cat1 != null)
-        return flatten(cls, tr.cat1) + flatten(cls, tr.cat2);
-    else if (tr.closure != null) {
+function flattenAcc(a, cls, tr) {
+    if (tr.cat1 != null) {
+        flattenAcc(a, cls, tr.cat1);
+        flattenAcc(a, cls, tr.cat2);
+    } else if (tr.closure != null) {
         var cl = newClosure(tr.closure);
         cls.v = cons(cl, cls.v);
-        return "cr(" + cl + ")";
+        a.push("cr(", cl.toString(), ")");
     } else
-        return tr;
+        a.push(tr);
+}
+
+function flatten(cls, tr) {
+    var a = [];
+    flattenAcc(a, cls, tr);
+    return a.join("");
 }
 
 function flattenLocal(s) {
@@ -1247,8 +1254,8 @@
 function uf(s) {
     if (s.length == 0)
         return "_";
-    s = s.replace(new RegExp ("\\.", "g"), ".2E");
-    return (s.charAt(0) == '_' ? "_" : "") + encodeURIComponent(s).replace(new RegExp ("%", "g"), ".");
+    s = s.replace(/\./g, ".2E");
+    return (s.charAt(0) == '_' ? "_" : "") + encodeURIComponent(s).replace(/%/g, ".");
 }
 
 function uu(s) {
@@ -1257,14 +1264,14 @@
     } else if (s.length >= 3 && (s.charAt(0) == '%' || s.charAt(0) == '.')
                && s.charAt(1) == '5' && (s.charAt(2) == 'f' || s.charAt(2) == 'F'))
         s = s.substring(3);
-    s = s.replace(new RegExp ("\\+", "g"), " ");
-    s = s.replace(new RegExp ("\\.", "g"), "%");
+    s = s.replace(/\+/g, " ");
+    s = s.replace(/\./g, "%");
     return decodeURIComponent(s);
 }
 
 function atr(s) {
-    return s.replace(new RegExp ("\"", "g"), "&quot;").replace(new RegExp ("&", "g"), "&amp;")
-        }
+    return s.replace(/\"/g, "&quot;").replace(/&/g, "&amp;")
+}
 
 function ub(b) {
     return b ? "1" : "0";