Do not buffer for simple programs (1 statement)

This commit is contained in:
kpdecker
2011-07-31 21:44:16 -05:00
parent 82e3344aed
commit d6b97cf34d
+21 -5
View File
@@ -127,6 +127,7 @@ Handlebars.JavaScriptCompiler = function() {};
statement = statements[i];
this[statement.type](statement);
}
this.isSimple = l === 1;
this.depths.list = this.depths.list.sort(function(a, b) {
return a - b;
@@ -323,7 +324,11 @@ Handlebars.JavaScriptCompiler = function() {};
},
appendToBuffer: function(string) {
return "buffer += " + string + ";";
if (this.environment.isSimple) {
return "return " + string + ";";
} else {
return "buffer += " + string + ";";
}
},
initializeBuffer: function() {
@@ -350,7 +355,7 @@ Handlebars.JavaScriptCompiler = function() {};
this.compileChildren(environment, options);
var opcodes = environment.opcodes, opcode, name, declareName, declareVal;
var opcodes = environment.opcodes, opcode;
this.i = 0;
@@ -406,7 +411,11 @@ Handlebars.JavaScriptCompiler = function() {};
out.push('');
}
out.push("var buffer = " + this.initializeBuffer());
if (!this.environment.isSimple) {
out.push(", buffer = " + this.initializeBuffer());
} else {
out.push("");
}
// track the last context pushed into place to allow skipping the
// getContext opcode when it would be a noop
@@ -432,14 +441,18 @@ Handlebars.JavaScriptCompiler = function() {};
}
}
this.source[1] = this.source[1] + ";";
if (this.source[1]) {
this.source[1] = "var " + this.source[1].substring(2) + ";";
}
// Merge children
if (!this.isChild) {
this.source[1] += '\n' + this.context.programs.join('\n') + '\n';
}
this.source.push("return buffer;");
if (!this.environment.isSimple) {
this.source.push("return buffer;");
}
var params = this.isChild ? ["depth0", "data"] : ["Handlebars", "depth0", "helpers", "partials", "data"];
@@ -467,6 +480,9 @@ Handlebars.JavaScriptCompiler = function() {};
append: function() {
var local = this.popStack();
this.source.push("if(" + local + " || " + local + " === 0) { " + this.appendToBuffer(local) + " }");
if (this.environment.isSimple) {
this.source.push("else { " + this.appendToBuffer("''") + " }");
}
},
appendEscaped: function() {