From 7a7ad74ff1c4fe8685d33cceebe03f1474eb865d Mon Sep 17 00:00:00 2001 From: kpdecker Date: Sun, 19 Jan 2014 18:54:13 -0600 Subject: [PATCH] Optimize buffer generate first and all edge cases --- .../compiler/javascript-compiler.js | 65 +++++++++++-------- spec/expected/empty.amd.js | 5 +- spec/javascript-compiler.js | 2 + 3 files changed, 43 insertions(+), 29 deletions(-) diff --git a/lib/handlebars/compiler/javascript-compiler.js b/lib/handlebars/compiler/javascript-compiler.js index 8222506d2..878181901 100644 --- a/lib/handlebars/compiler/javascript-compiler.js +++ b/lib/handlebars/compiler/javascript-compiler.js @@ -140,42 +140,27 @@ JavaScriptCompiler.prototype = { }, preamble: function() { - var out = []; - - 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 this.lastContext = 0; - this.source = out; + this.source = []; }, createFunctionContext: function(asObject) { - var locals = this.stackVars.concat(this.registers.list); + var varDeclarations = ''; + var locals = this.stackVars.concat(this.registers.list); if(locals.length > 0) { - this.source[0] += ", " + locals.join(", "); + varDeclarations += ", " + locals.join(", "); } // Generate minimizer alias mappings for (var alias in this.aliases) { if (this.aliases.hasOwnProperty(alias)) { - this.source[0] += ', ' + alias + '=' + this.aliases[alias]; + varDeclarations += ', ' + alias + '=' + this.aliases[alias]; } } - if (this.source[0]) { - this.source[0] = "var " + this.source[0].substring(2) + ";"; - } - - if (!this.environment.isSimple) { - this.pushSource("return buffer;"); - } - var params = ["depth0", "helpers", "partials", "data"]; for(var i=0, l=this.environment.depths.list.length; i= 1.0.0"],"main":function(depth0,helpers,partials,data) { - var buffer = ""; - return buffer; - },"useData":true}); + return ""; +},"useData":true}); }); diff --git a/spec/javascript-compiler.js b/spec/javascript-compiler.js index a9ae0f8a9..16058680b 100644 --- a/spec/javascript-compiler.js +++ b/spec/javascript-compiler.js @@ -45,10 +45,12 @@ describe('javascript-compiler api', function() { describe('buffer', function() { var $superAppend, $superCreate; beforeEach(function() { + handlebarsEnv.JavaScriptCompiler.prototype.forceBuffer = true; $superAppend = handlebarsEnv.JavaScriptCompiler.prototype.appendToBuffer; $superCreate = handlebarsEnv.JavaScriptCompiler.prototype.initializeBuffer; }); afterEach(function() { + handlebarsEnv.JavaScriptCompiler.prototype.forceBuffer = false; handlebarsEnv.JavaScriptCompiler.prototype.appendToBuffer = $superAppend; handlebarsEnv.JavaScriptCompiler.prototype.initializeBuffer = $superCreate; });