From 0af125f3fc9cdf2a9c320ae5f0778dd4e3671261 Mon Sep 17 00:00:00 2001 From: Vladimir Shabanov Date: Thu, 30 Aug 2012 17:58:37 +0400 Subject: Minor js optimizations. * flatten() now uses accumulator and join. * reuse regexps instead of allocating new each time --- lib/js/urweb.js | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/js/urweb.js b/lib/js/urweb.js index 6e67cde7..a7fed038 100644 --- a/lib/js/urweb.js +++ b/lib/js/urweb.js @@ -595,15 +595,22 @@ function cr(n) { 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 parseSource(s1, s2) { 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 @@ function uu(s) { } 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"; -- cgit v1.2.3