Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed "cache" option. ~4x performance increase
my bad
  • Loading branch information
tj committed Oct 7, 2010
1 parent 76cf948 commit 7e1af95
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Expand Up @@ -36,7 +36,7 @@ Embedded JavaScript templates.
- `locals` Local variables object
- `cache` Compiled functions are cached, requires `filename`
- `filename` Used by `cache` to key caches
- `context|scope` Function execution context
- `scope` Function execution context
- `debug` Output generated function body
- `open` Open tag, defaulting to "<%"
- `close` Closing tag, defaulting to "%>"
Expand Down
4 changes: 2 additions & 2 deletions benchmark.js
Expand Up @@ -2,13 +2,13 @@

var ejs = require('./lib/ejs'),
str = '<% if (foo) { %><p><%= foo %></p><% } %>',
times = 5000;
times = 50000;

console.log('rendering ' + times + ' times');

var start = new Date;
while (times--) {
ejs.render(str, { locals: { foo: 'bar' }});
ejs.render(str, { cache: true, filename: 'test', locals: { foo: 'bar' }});
}

console.log('took ' + (new Date - start) + 'ms');
8 changes: 4 additions & 4 deletions lib/ejs/index.js
Expand Up @@ -136,8 +136,8 @@ var parse = exports.parse = function(str, options){

var compile = exports.compile = function(str, options){
if (options.debug) sys.puts(parse(str));
var fn = new Function('locals, filters, escape', parse(str, options));
return function(locals){
var fn = new Function('locals, filters, escape', parse(str, options));
return fn.call(this, locals, filters, utils.escape);
}
};
Expand All @@ -150,7 +150,7 @@ var compile = exports.compile = function(str, options){
* - `locals` Local variables object
* - `cache` Compiled functions are cached, requires `filename`
* - `filename` Used by `cache` to key caches
* - `context|scope` Function execution context
* - `scope` Function execution context
* - `debug` Output generated function body
* - `open` Open tag, defaulting to "<%"
* - `close` Closing tag, defaulting to "%>"
Expand All @@ -166,12 +166,12 @@ exports.render = function(str, options){
options = options || {};
if (options.cache) {
if (options.filename) {
fn = cache[options.filename] = compile(str, options);
fn = cache[options.filename] || (cache[options.filename] = compile(str, options));
} else {
throw new Error('"cache" option requires "filename".');
}
} else {
fn = compile(str, options);
}
return fn.call(options.context || options.scope, options.locals || {});
return fn.call(options.scope, options.locals || {});
};
6 changes: 0 additions & 6 deletions test/ejs.test.js
Expand Up @@ -34,12 +34,6 @@ module.exports = {
assert.equal(html, ejs.render(str, { scope: 'tj' }));
},

'test `context` option': function(assert){
var html = '<p>tj</p>',
str = '<p><%= this %></p>';
assert.equal(html, ejs.render(str, { context: 'tj' }));
},

'test escaping': function(assert){
assert.equal('&lt;script&gt;', ejs.render('<%= "<script>" %>'));
assert.equal('<script>', ejs.render('<%- "<script>" %>'));
Expand Down

0 comments on commit 7e1af95

Please sign in to comment.