Fixed performance issue in has_parens()

This commit is contained in:
Tobias 2018-04-04 18:05:36 +02:00
parent 81603ecd15
commit 7f4464d8d3

View File

@ -109,6 +109,7 @@ function OutputStream(options) {
var current_line = 1;
var current_pos = 0;
var OUTPUT = "";
var LAST_OUTPUT_FRAGMENT = "";
var to_utf8 = options.ascii_only ? function(str, identifier) {
return str.replace(/[\u0000-\u001f\u007f-\uffff]/g, function(ch) {
@ -233,7 +234,7 @@ function OutputStream(options) {
if (current_col > options.max_line_len) {
if (might_add_newline) {
var left = OUTPUT.slice(0, might_add_newline);
var right = OUTPUT.slice(might_add_newline);
var right = LAST_OUTPUT_FRAGMENT = OUTPUT.slice(might_add_newline);
if (mappings) {
var delta = right.length - current_col;
mappings.forEach(function(mapping) {
@ -339,7 +340,7 @@ function OutputStream(options) {
if (!might_add_newline) do_add_mapping();
}
OUTPUT += str;
OUTPUT += LAST_OUTPUT_FRAGMENT = str;
current_pos += str.length;
var a = str.split(/\r?\n/), n = a.length - 1;
current_line += n;
@ -375,7 +376,7 @@ function OutputStream(options) {
var newline = options.beautify ? function() {
if (newline_insert < 0) return print("\n");
if (OUTPUT[newline_insert] != "\n") {
OUTPUT = OUTPUT.slice(0, newline_insert) + "\n" + OUTPUT.slice(newline_insert);
OUTPUT = OUTPUT.slice(0, newline_insert) + "\n" + (LAST_OUTPUT_FRAGMENT = OUTPUT.slice(newline_insert));
current_pos++;
current_line++;
}
@ -576,7 +577,7 @@ function OutputStream(options) {
indentation : function() { return indentation },
current_width : function() { return current_col - indentation },
should_break : function() { return options.width && this.current_width() >= options.width },
has_parens : function() { return OUTPUT[OUTPUT.length - 1] == "(" },
has_parens : function() { return LAST_OUTPUT_FRAGMENT[LAST_OUTPUT_FRAGMENT.length - 1] == "(" },
newline : newline,
print : print,
space : space,