Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed "cache" option. ~4x performance increase

my bad
  • Loading branch information...
commit 7e1af95d2ab2c480c20b0a8cc80cad027c4e9896 1 parent 76cf948
@tj authored
View
2  Readme.md
@@ -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 "%>"
View
4 benchmark.js
@@ -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');
View
8 lib/ejs/index.js
@@ -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);
}
};
@@ -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 "%>"
@@ -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 || {});
};
View
6 test/ejs.test.js
@@ -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>" %>'));
Please sign in to comment.
Something went wrong with that request. Please try again.