Permalink
Browse files

benchmark/compile

  • Loading branch information...
1 parent fe72073 commit de0f9fd81afcd1e3c48ad5a88e07d97257787d48 @leizongmin committed Nov 22, 2014
Showing with 88 additions and 13 deletions.
  1. +73 −0 benchmark/compile.js
  2. +12 −12 benchmark/index.js
  3. +1 −0 benchmark/tpl.ejs
  4. +1 −1 benchmark/tpl.liquid
  5. +1 −0 package.json
View
@@ -0,0 +1,73 @@
+var fs = require('fs');
+var async = require('async');
+var tinyliquid = require('../');
+var ejs = require('ejs');
+
+var TPL_EJS = fs.readFileSync(__dirname + '/tpl.ejs').toString();
+var TPL_LIQUID = fs.readFileSync(__dirname + '/tpl.liquid').toString();
+
+for (var i = 0; i < 10; i++) {
+ TPL_LIQUID += '\n' + TPL_LIQUID;
+ TPL_EJS += '\n' + TPL_EJS;
+}
+
+
+var LOOP_NUM = 100;
+
+var RESULTS_EJS = [];
+var RESULTS_LIQUID = [];
+
+function getAverage (list) {
+ var sum = list.reduce(function (a, b) {
+ return a + b;
+ });
+ return sum / list.length;
+}
+
+
+function compileLiquid (callback) {
+
+ for (var i = 0; i < LOOP_NUM; i++) {
+ var timestamp = Date.now();
+ var ast = tinyliquid.parse(TPL_LIQUID);
+ var spent = Date.now() - timestamp;
+ RESULTS_LIQUID.push(spent);
+ }
+
+ callback && callback();
+}
+
+function compileEjs (callback) {
+
+ for (var i = 0; i < LOOP_NUM; i++) {
+ var timestamp = Date.now();
+ var render = ejs.compile(TPL_EJS);
+ var spent = Date.now() - timestamp;
+ RESULTS_EJS.push(spent);
+ }
+
+ callback && callback();
+}
+
+
+async.series([
+ function (done) {
+ compileLiquid(function () {
+ console.log('Average: %sms', getAverage(RESULTS_LIQUID));
+ console.log('---------');
+ done();
+ });
+ },
+ function (done) {
+ compileEjs(function () {
+ console.log('Average: %sms', getAverage(RESULTS_EJS));
+ console.log('---------');
+ done();
+ });
+ }
+], function (err) {
+ if (err) throw err;
+ console.log('tinyliquid Average: %sms', getAverage(RESULTS_LIQUID));
+ console.log('EJS Average: %sms', getAverage(RESULTS_EJS));
+ console.log('tinyliquid is %sx slower than EJS', (getAverage(RESULTS_LIQUID) / getAverage(RESULTS_EJS)).toFixed(1));
+});
View
@@ -1,14 +1,14 @@
var fs = require('fs');
var async = require('async');
-var TinyLiquid = require('../');
-require('../other/ejs');
+var tinyliquid = require('../');
+var ejs = require('ejs');
var TPL_EJS = fs.readFileSync(__dirname + '/tpl.ejs').toString();
var TPL_LIQUID = fs.readFileSync(__dirname + '/tpl.liquid').toString();
-var _data = Object.keys(TinyLiquid).map(function (k) {
- return {name: k, value: TinyLiquid[k]};
+var _data = Object.keys(tinyliquid).map(function (k) {
+ return {name: k, value: tinyliquid[k]};
}).slice(0, 10);
var data = [];
for (var i = 0; i < 2000; i++) {
@@ -33,22 +33,22 @@ function getAverage (list) {
function renderLiquid (callback) {
- var ast = TinyLiquid.parse(TPL_LIQUID);
- var c = TinyLiquid.newContext();
+ var ast = tinyliquid.parse(TPL_LIQUID);
+ var c = tinyliquid.newContext();
c.setLocals('list', data);
c.setFilter('to_string', function (v) {
return typeof v === 'function' ? '[Function]' : v;
});
var timestamp = Date.now();
- TinyLiquid.run(ast, c, function (err) {
+ tinyliquid.run(ast, c, function (err) {
if (err) throw err;
var spent = Date.now() - timestamp;
RESULTS_LIQUID.push(spent);
- console.log('TinyLiquid: total ' + data.length + ' items, spent ' + spent + 'ms');
+ console.log('tinyliquid: total ' + data.length + ' items, spent ' + spent + 'ms');
callback && callback();
});
@@ -82,7 +82,7 @@ async.series([
renderLiquid(next);
}, function (err) {
if (err) return next(err);
- console.log('Average: ' + getAverage(RESULTS_LIQUID));
+ console.log('Average: %sms', getAverage(RESULTS_LIQUID));
console.log('---------');
done();
});
@@ -92,14 +92,14 @@ async.series([
renderEjs(next);
}, function (err) {
if (err) return next(err);
- console.log('Average: ' + getAverage(RESULTS_EJS));
+ console.log('Average: %sms', getAverage(RESULTS_EJS));
console.log('---------');
done();
});
}
], function (err) {
if (err) throw err;
- console.log('TinyLiquid Average: %sms', getAverage(RESULTS_LIQUID));
+ console.log('tinyliquid Average: %sms', getAverage(RESULTS_LIQUID));
console.log('EJS Average: %sms', getAverage(RESULTS_EJS));
- console.log('TinyLiquid is %sx slower than EJS', (getAverage(RESULTS_LIQUID) / getAverage(RESULTS_EJS)).toFixed(1));
+ console.log('tinyliquid is %sx slower than EJS', (getAverage(RESULTS_LIQUID) / getAverage(RESULTS_EJS)).toFixed(1));
});
View
@@ -3,3 +3,4 @@
<li><%= item.name %> = <%= to_string(item.value) %></li>
<% }); %>
</ul>
+<ul>
@@ -1,5 +1,5 @@
<ul>
{% for item in list %}
- <li>{{item.name|escape}} = {{item.value|to_string}}</li>
+ <li>{{item.name|escape}} = {{item.value|to_string|escape}}</li>
{% endfor %}
</ul>
View
@@ -35,6 +35,7 @@
"async": "^0.9.0",
"blanket": "~1.1.5",
"browserify": "*",
+ "ejs": "^1.0.0",
"mocha": "~1.8.1",
"uglify-js": "*"
},

0 comments on commit de0f9fd

Please sign in to comment.