Permalink
Browse files

Merge pull request #217 from masylum/master

Optional `self` object to avoid poor `with` performance
  • Loading branch information...
2 parents db3f8f7 + 2b97951 commit 5a4cba97bd52aa0eba35b0f7b5b8e020db12bdf4 @tj tj committed May 14, 2011
Showing with 53 additions and 2 deletions.
  1. +1 −0 Makefile
  2. +1 −0 Readme.md
  3. +6 −0 benchmarks/example-self.jade
  4. +43 −0 benchmarks/jade-self.js
  5. +2 −2 lib/jade.js
View
1 Makefile
@@ -14,6 +14,7 @@ test:
benchmark:
@node benchmarks/jade.js \
+ && node benchmarks/jade-self.js \
&& node benchmarks/haml.js \
&& node benchmarks/haml2.js \
&& node benchmarks/ejs.js
View
1 Readme.md
@@ -75,6 +75,7 @@ via npm:
### Options
- `scope` Evaluation scope (`this`)
+ - `self` Use a `self` namespace to hold the locals. _false by default_
- `locals` Local variable object
- `filename` Used in exceptions, and required by `cache`
- `cache` Cache intermediate JavaScript in memory keyed by `filename`
View
6 benchmarks/example-self.jade
@@ -0,0 +1,6 @@
+ul
+ li!= self.one
+ li!= self.two
+ li!= self.three
+ - each item in self.items
+ li!= item
View
43 benchmarks/jade-self.js
@@ -0,0 +1,43 @@
+
+/**
+ * Module dependencies.
+ */
+
+var bm = require('./common'),
+ jade = require('../lib/jade'),
+ fs = require('fs');
+
+var str = fs.readFileSync(__dirname + '/example-self.jade', 'ascii');
+var fn = jade.compile(str, {self: true});
+var n = bm.times;
+
+bm.start('jade compilation');
+while (n--) {
+ jade.render(str, {
+ filename: 'example-self.jade'
+ , self: true
+ , locals: bm.locals
+ });
+}
+bm.stop();
+
+var n = bm.times;
+
+bm.start('jade execution');
+while (n--) {
+ jade.render(str, {
+ filename: 'example-self.jade'
+ , self: true
+ , cache: true
+ , locals: bm.locals
+ });
+}
+bm.stop();
+
+var n = bm.times;
+
+bm.start('jade compile()');
+while (n--) {
+ fn(bm.locals);
+}
+bm.stop();
View
4 lib/jade.js
@@ -181,7 +181,7 @@ function parse(str, options){
+ attrs.toString() + '\n\n'
+ escape.toString() + '\n\n'
+ 'var buf = [];\n'
- + 'with (locals || {}) {' + js + '}'
+ + (options.self ? 'var self = locals || {}, __ = locals.__;\n' + js : 'with (locals || {}) {' + js + '}')
+ 'return buf.join("");';
} catch (err) {
process.compile(js, filename || 'Jade');
@@ -306,4 +306,4 @@ exports.renderFile = function(path, options, fn){
}
});
}
-};
+};

0 comments on commit 5a4cba9

Please sign in to comment.