Skip to content

Commit 0cbb42f

Browse files
committed
Optimization and performance improvements.
1 parent 3ba8bc9 commit 0cbb42f

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

compile.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@
3333
ast;
3434
// Extend the Templating engine with a print method for the generated functions:
3535
tmpl.print = function (str) {
36-
var helper = helperRegexp.test(str) ? tmpl.helper : "";
36+
// Only add helper functions if they are used inside of the template:
37+
var helper = helperRegexp.test(str) ? tmpl.helper : "",
38+
body = str.replace(tmpl.regexp, tmpl.func);
39+
if (helper || (/_e\s*\(/.test(body))) {
40+
helper = "_e=tmpl.encode" + helper + ",";
41+
}
3742
return "function(" + tmpl.arg + ",tmpl){" +
38-
("var _s='',_e=tmpl.encode" + helper + ";_s+='" +
39-
str.replace(tmpl.regexp, tmpl.func) +
40-
"';return _s;").split("_s+='';").join("") +
41-
"}";
43+
("var " + helper + "_s='" + body + "';return _s;")
44+
.split("_s+='';").join("") + "}";
4245
};
4346
// Loop through the command line arguments:
4447
process.argv.forEach(function (file, index) {

tmpl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
tmpl(tmpl.load(str)) :
2323
new Function(
2424
tmpl.arg + ',tmpl',
25-
("var _s='',_e=tmpl.encode" + tmpl.helper + ";_s+='" +
25+
("var _e=tmpl.encode" + tmpl.helper + ",_s='" +
2626
str.replace(tmpl.regexp, tmpl.func) +
27-
"';return _s;").split("_s+='';").join("")
27+
"';return _s;")
2828
);
2929
return data ? f(data, tmpl) : function (data) {
3030
return f(data, tmpl);

tmpl.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)