Skip to content

Commit

Permalink
Merge pull request mde#272 from User4martin/optimize_include_performance
Browse files Browse the repository at this point in the history
use only one __output array for template and includes
  • Loading branch information
mde committed Jul 16, 2017
2 parents 6f8421b + e32869d commit 8aaa120
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions lib/ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,12 +481,20 @@ Template.prototype = {

if (!this.source) {
this.generateSource();
prepended += ' var __output = [], __append = __output.push.bind(__output);' + '\n';
if (opts.client) {
prepended += 'var __output = [], __append = callerAppend || __output.push.bind(__output);' + '\n';
}
else {
prepended += 'var __append = callerAppend;' + '\n';
}

if (opts._with !== false) {
prepended += ' with (' + opts.localsName + ' || {}) {' + '\n';
appended += ' }' + '\n';
}
appended += ' return __output.join("");' + '\n';
if (opts.client) {
appended += ' if (! callerAppend) { return __output.join("");} ' + '\n';
}
this.source = prepended + this.source + appended;
}

Expand Down Expand Up @@ -521,7 +529,7 @@ Template.prototype = {
}

try {
fn = new Function(opts.localsName + ', escapeFn, include, rethrow', src);
fn = new Function(opts.localsName + ', escapeFn, include, rethrow, callerAppend', src);
}
catch(e) {
// istanbul ignore else
Expand All @@ -544,15 +552,21 @@ Template.prototype = {
// Return a callable function which will execute the function
// created by the source-code, with the passed data as locals
// Adds a local `include` function which allows full recursive include
var returnedFn = function (data) {
var returnedFn = function (data, callerAppend) {
var __output, __append = callerAppend;
if (! callerAppend) {
__output = [];
__append = __output.push.bind(__output);
}
var include = function (path, includeData) {
var d = utils.shallowCopy({}, data);
if (includeData) {
d = utils.shallowCopy(d, includeData);
}
return includeFile(path, opts)(d);
return includeFile(path, opts)(d, __append);
};
return fn.apply(opts.context, [data || {}, escapeFn, include, rethrow]);
fn.apply(opts.context, [data || {}, escapeFn, include, rethrow, __append]);
if (! callerAppend) { return __output.join(''); }
};
returnedFn.dependencies = this.dependencies;
return returnedFn;
Expand Down

0 comments on commit 8aaa120

Please sign in to comment.