# HG changeset patch # User Vladimir Shabanov # Date 1346335117 -14400 # Node ID 4c61bb7acb9bf0956180c8d2deefce1c8c7814da # Parent ea5f920ffa35eac8a8157763b41f5e7ff1330075 Minor js optimizations. * flatten() now uses accumulator and join. * reuse regexps instead of allocating new each time diff -r ea5f920ffa35 -r 4c61bb7acb9b lib/js/urweb.js --- 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"), """).replace(new RegExp ("&", "g"), "&") - } + return s.replace(/\"/g, """).replace(/&/g, "&") +} function ub(b) { return b ? "1" : "0";