Permalink
Browse files

Merge pull request #591 from chowey/patch-4

Improve performance of pretty mixins
  • Loading branch information...
2 parents e5da902 + e766f10 commit 36349d9ddf1daa4a167427e915febb7d5f6ffb00 @tj tj committed Apr 15, 2012
Showing with 15 additions and 9 deletions.
  1. +15 −9 lib/compiler.js
View
@@ -55,6 +55,7 @@ var Compiler = module.exports = function Compiler(node, options) {
this.pp = options.pretty || false;
this.debug = false !== options.compileDebug;
this.indents = 0;
+ this.parentIndents = 0;
if (options.doctype) this.setDoctype(options.doctype);
};
@@ -72,7 +73,7 @@ Compiler.prototype = {
compile: function(){
this.buf = ['var interp;'];
- if (this.pp) this.buf.push("var __indent = ['\\n'];");
+ if (this.pp) this.buf.push("var __indent = [];");
this.lastBufferedIdx = -1;
this.visit(this.node);
return this.buf.join('\n');
@@ -250,12 +251,14 @@ Compiler.prototype = {
if (mixin.block) {
this.buf.push('var ' + name + ' = function(' + args + '){');
- if (this.pp) this.buf.push("__indent.push(' ');")
+ if (this.pp) this.parentIndents++;
this.visit(mixin.block);
- if (this.pp) this.buf.push("__indent.pop();")
+ if (this.pp) this.parentIndents--;
this.buf.push('}');
} else {
+ if (this.pp) this.buf.push("__indent.push('" + Array(this.indents + 1).join(' ') + "');")
this.buf.push(name + '(' + args + ');');
+ if (this.pp) this.buf.push("__indent.pop();")
}
},
@@ -270,7 +273,6 @@ Compiler.prototype = {
visitTag: function(tag){
this.indents++;
var name = tag.name;
- if (this.pp && this.indents > 1) this.buf.push("__indent.push(' ');");
if (!this.hasCompiledTag) {
if (!this.hasCompiledDoctype && 'html' == name) {
@@ -281,7 +283,9 @@ Compiler.prototype = {
// pretty print
if (this.pp && inlineTags.indexOf(name) == -1) {
- this.buf.push("buf.push.apply(buf, __indent);");
+ this.buffer('\\n' + Array(this.indents).join(' '));
+ if (this.parentIndents)
+ this.buf.push("buf.push.apply(buf, __indent);");
}
if (~selfClosing.indexOf(name) && !this.xml) {
@@ -306,12 +310,13 @@ Compiler.prototype = {
// pretty print
if (this.pp && !~inlineTags.indexOf(name) && !tag.textOnly && !tag.isText()) {
- this.buf.push("buf.push.apply(buf, __indent);");
+ this.buffer('\\n' + Array(this.indents).join(' '));
+ if (this.parentIndents)
+ this.buf.push("buf.push.apply(buf, __indent);");
}
this.buffer('</' + name + '>');
}
- if (this.pp && this.indents > 1) this.buf.push('__indent.pop();');
this.indents--;
},
@@ -367,8 +372,9 @@ Compiler.prototype = {
visitComment: function(comment){
if (!comment.buffer) return;
if (this.pp) {
- this.buf.push("buf.push.apply(buf, __indent);");
- if (this.indents > 0) this.buffer(' ');
+ this.buffer('\\n' + Array(this.indents + 1).join(' '));
+ if (this.parentIndents)
+ this.buf.push("buf.push.apply(buf, __indent);");
}
this.buffer('<!--' + utils.escape(comment.val) + '-->');
},

0 comments on commit 36349d9

Please sign in to comment.