diff options
author | Mihai Bazon <mihai@bazon.net> | 2012-08-17 19:04:23 +0300 |
---|---|---|
committer | Mihai Bazon <mihai@bazon.net> | 2012-08-17 19:04:23 +0300 |
commit | ef87c9fd8fafa006146d45ba2a870237a127c9d4 (patch) | |
tree | 766e7507d44f60e9174878065c8ca4ed0363161b | |
parent | 901f77047e581c79132f93451f057629788d0e9f (diff) | |
download | tracifyjs-ef87c9fd8fafa006146d45ba2a870237a127c9d4.tar.gz tracifyjs-ef87c9fd8fafa006146d45ba2a870237a127c9d4.zip |
big speed improvement (observable when beautify = false)
who would have thought that str.charAt(str.length - 1) is not a constant,
instant operation? seems to get slower and slower as the string grows.
0.6s vs. 3s
-rw-r--r-- | lib/output.js | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/lib/output.js b/lib/output.js index 93aa7f6b..12cf104e 100644 --- a/lib/output.js +++ b/lib/output.js @@ -68,22 +68,21 @@ function OutputStream(options) { return repeat_string(" ", options.indent_start + indentation - back * options.indent_level); }; - function last_char() { - return OUTPUT.charAt(OUTPUT.length - 1); - }; - /* -----[ beautification/minification ]----- */ var might_need_space = false; var might_need_semicolon = false; var last = null; + function last_char() { + return last.charAt(last.length - 1); + }; + function print(str) { - last = str; str = String(str); var ch = str.charAt(0); if (might_need_semicolon) { - if (";}".indexOf(ch) < 0 && !/[;]$/.test(OUTPUT)) { + if (";}".indexOf(ch) < 0 && !/[;]$/.test(last)) { OUTPUT += ";"; current_col++; current_pos++; @@ -96,7 +95,7 @@ function OutputStream(options) { if ((is_identifier_char(last_char()) && (is_identifier_char(ch) || ch == "\\")) || - (/[\+\-]$/.test(OUTPUT) && /^[\+\-]/.test(str))) + (/[\+\-]$/.test(last) && /^[\+\-]/.test(str))) { OUTPUT += " "; current_col++; @@ -112,6 +111,7 @@ function OutputStream(options) { current_col += a[n - 1].length; } current_pos += str.length; + last = str; OUTPUT += str; }; @@ -682,8 +682,9 @@ function OutputStream(options) { output.print("]"); }); DEFPRINT(AST_UnaryPrefix, function(self, output){ - output.print(self.operator); - if (is_alphanumeric_char(self.operator.charAt(0))) + var op = self.operator; + output.print(op); + if (/^[a-z]/i.test(op)) output.space(); self.expression.print(output); }); |