Permalink
Browse files

benchmark: about 3.5x slower than ejs

  • Loading branch information...
1 parent 85f3d3f commit fe72073c10926d34b61387293eb07a18b84dfa52 @leizongmin committed Nov 21, 2014
Showing with 147 additions and 25 deletions.
  1. +0 −1 .gitignore
  2. +105 −0 benchmark/index.js
  3. +5 −0 benchmark/tpl.ejs
  4. +5 −0 benchmark/tpl.liquid
  5. +32 −24 package.json
View
@@ -1,6 +1,5 @@
node_modules
examples
-benchmark
lib-cov
coverage.html
*.log
View
@@ -0,0 +1,105 @@
+var fs = require('fs');
+var async = require('async');
+var TinyLiquid = require('../');
+require('../other/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]};
+}).slice(0, 10);
+var data = [];
+for (var i = 0; i < 2000; i++) {
+ data = data.concat(_data);
+}
+
+var loop = [];
+for (var i = 1; i <= 20; i++) {
+ loop.push(i);
+}
+
+var RESULTS_EJS = [];
+var RESULTS_LIQUID = [];
+
+function getAverage (list) {
+ var sum = list.reduce(function (a, b) {
+ return a + b;
+ });
+ return sum / list.length;
+}
+
+
+function renderLiquid (callback) {
+
+ 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) {
+ if (err) throw err;
+
+ var spent = Date.now() - timestamp;
+ RESULTS_LIQUID.push(spent);
+
+ console.log('TinyLiquid: total ' + data.length + ' items, spent ' + spent + 'ms');
+
+ callback && callback();
+ });
+}
+
+function renderEjs (callback) {
+
+ var render = ejs.compile(TPL_EJS);
+
+ var timestamp = Date.now();
+
+ var html = render({
+ data: data,
+ to_string: function (v) {
+ return typeof v === 'function' ? '[Function]' : v;
+ }
+ });
+
+ var spent = Date.now() - timestamp;
+ RESULTS_EJS.push(spent);
+
+ console.log('EJS: total ' + data.length + ' items, spent ' + spent + 'ms');
+
+ callback && callback();
+}
+
+
+async.series([
+ function (done) {
+ async.eachSeries(loop, function (item, next) {
+ renderLiquid(next);
+ }, function (err) {
+ if (err) return next(err);
+ console.log('Average: ' + getAverage(RESULTS_LIQUID));
+ console.log('---------');
+ done();
+ });
+ },
+ function (done) {
+ async.eachSeries(loop, function (item, next) {
+ renderEjs(next);
+ }, function (err) {
+ if (err) return next(err);
+ console.log('Average: ' + 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
@@ -0,0 +1,5 @@
+<ul>
+<% data.forEach(function (item) { %>
+ <li><%= item.name %> = <%= to_string(item.value) %></li>
+<% }); %>
+</ul>
@@ -0,0 +1,5 @@
+<ul>
+{% for item in list %}
+ <li>{{item.name|escape}} = {{item.value|to_string}}</li>
+{% endfor %}
+</ul>
View
@@ -1,39 +1,47 @@
{
- "name": "tinyliquid",
- "main": "./lib/index.js",
- "version": "0.2.26",
- "description": "A liquid template engine",
- "keywords": ["liquid", "template"],
- "author": "Zongmin Lei <leizongmin@gmail.com>",
+ "name": "tinyliquid",
+ "main": "./lib/index.js",
+ "version": "0.2.26",
+ "description": "A liquid template engine",
+ "keywords": [
+ "liquid",
+ "template"
+ ],
+ "author": "Zongmin Lei <leizongmin@gmail.com>",
"contributors": [
- {"name": "Zongmin Lei", "email": "leizongmin@gmail.com"}
+ {
+ "name": "Zongmin Lei",
+ "email": "leizongmin@gmail.com"
+ }
],
"repository": {
- "type": "git",
- "url": "git://github.com/leizongmin/tinyliquid.git"
+ "type": "git",
+ "url": "git://github.com/leizongmin/tinyliquid.git"
},
- "licenses": [{
- "type": "MIT",
- "url": "https://raw.github.com/leizongmin/tinyliquid/master/MIT-License"
- }],
- "bugs":{
- "url": "https://github.com/leizongmin/tinyliquid/issues"
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "https://raw.github.com/leizongmin/tinyliquid/master/MIT-License"
+ }
+ ],
+ "bugs": {
+ "url": "https://github.com/leizongmin/tinyliquid/issues"
},
"engines": {
- "node": ">= 0.6.0"
+ "node": ">= 0.6.0"
},
- "dependencies": {},
+ "dependencies": {},
"devDependencies": {
- "mocha": "~1.8.1",
- "async": "~0.2.9",
- "blanket": "~1.1.5",
- "browserify": "*",
- "uglify-js": "*"
+ "async": "^0.9.0",
+ "blanket": "~1.1.5",
+ "browserify": "*",
+ "mocha": "~1.8.1",
+ "uglify-js": "*"
},
"scripts": {
- "blanket": {
+ "blanket": {
"pattern": "tinyliquid/lib"
},
- "test": "mocha --require blanket -R html-cov > coverage.html -t 5000"
+ "test": "mocha --require blanket -R html-cov > coverage.html -t 5000"
}
}

0 comments on commit fe72073

Please sign in to comment.