Skip to content

Commit 4434314

Browse files
committed
Use string concatenation to build the output string
Switching from pushing to an array and using .join() to using simple string concatenation yields performance gains up to 260%.
1 parent da51ca3 commit 4434314

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

benchmark/bench-ejs.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

33
var ejs = require('..');
4+
var path = require('path');
45

5-
ejs.fileLoader = function(n) { return files[n.replace(/^\//, '').replace(/\.ejs$/, '')]; };
6+
ejs.fileLoader = function(n) { return files[path.basename(n, '.ejs')]; };
67

78
var loops = 10000;
89
var runs = 9; // min 4 for median

lib/ejs.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,9 @@ Template.prototype = {
572572

573573
if (!this.source) {
574574
this.generateSource();
575-
prepended += ' var __output = [], __append = __output.push.bind(__output);' + '\n';
575+
prepended +=
576+
' var __output = "";\n' +
577+
' function __append(s) { if (s !== undefined && s !== null) __output += s }\n';
576578
if (opts.outputFunctionName) {
577579
prepended += ' var ' + opts.outputFunctionName + ' = __append;' + '\n';
578580
}
@@ -591,7 +593,7 @@ Template.prototype = {
591593
prepended += ' with (' + opts.localsName + ' || {}) {' + '\n';
592594
appended += ' }' + '\n';
593595
}
594-
appended += ' return __output.join("");' + '\n';
596+
appended += ' return __output;' + '\n';
595597
this.source = prepended + this.source + appended;
596598
}
597599

0 commit comments

Comments
 (0)