Skip to content

Commit

Permalink
Merge b15753f into 8740f2e
Browse files Browse the repository at this point in the history
  • Loading branch information
tbranyen committed May 25, 2015
2 parents 8740f2e + b15753f commit 5dda3d9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
31 changes: 30 additions & 1 deletion lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ define(function(require, exports, module) {
this.tree = tree;
this.string = "";

var compiledSource = this.process(this.tree.nodes);
// Optimization pass will flatten large templates to result in faster
// rendering.
var nodes = this.optimize(this.tree.nodes);
var compiledSource = this.process(nodes);

// The compiled function body.
var body = [];
Expand Down Expand Up @@ -134,6 +137,32 @@ define(function(require, exports, module) {
].join("\n");
}

/**
* Takes a pass over the full token set to optimize and eliminate potential
* bugs associated with super large templates.
*
* @memberOf module:compiler.Compiler
* @param {array} nodes
* @return {array} optimized nodes
*/
Compiler.prototype.optimize = function(nodes) {
return nodes.reduce(function(memo, node) {
var previous = memo[memo.length - 1];

// Optimize text nodes together.
if (previous && previous.type === "Text" && node.type === "Text") {
previous.value += node.value;
}

// This is the first item
else {
memo.push(node);
}

return memo;
}, []);
};

/**
* A recursively called method to detect how to compile each Node in the
* Tree.
Expand Down
2 changes: 1 addition & 1 deletion test/tests/lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ define(function(require, exports, module) {

describe("Compiler", function() {
it("is a constructor", function() {
assert.ok(typeof Compiler === "function");
assert.ok(typeof Compiler === "function");
});
});
});
9 changes: 9 additions & 0 deletions test/tests/tokenParsing.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,14 @@ define(function(require, exports, module) {

assert.equal(output, "'hello world'");
});

it("will not error on large files", function() {
var largeFile = new Array(10000).join('<test>');

var template = combyne(largeFile);
var output = template.render();

assert.equal(output, largeFile);
});
});
});

0 comments on commit 5dda3d9

Please sign in to comment.