Skip to content

Instantly share code, notes, and snippets.

@TimothyGu
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TimothyGu/2c43a88789d5302d81f9 to your computer and use it in GitHub Desktop.
Save TimothyGu/2c43a88789d5302d81f9 to your computer and use it in GitHub Desktop.
EJS benchmark
var suite = new (require('benchmark').Suite)
, ejs1 = require('ejs') // npm install ejs@1.0.0
, ejs2 = require('./../ejs') // npm install ejs@2.2.3
, newEjs2 = require('./../ejs-tj') // npm install ejs-tj@2.2.3-beta.1
var str = ' <% for (var i = 0; i < data.length; i++) { %>\n'
+ ' <% var objects = data[i] %>\n'
+ ' <h1><%= objects.title %></h1>\n'
+ ' <% for (var j = 0; j < objects.length; j++) { %>\n'
+ ' <% var obj = objects[j] %>\n'
+ ' <% if (obj.passed) { %>\n'
+ ' <p>Passed: <%= obj.name %></p>\n'
+ ' <p>Value: <%= obj.value %></p>\n'
+ ' <% } else { %>\n'
+ ' <p>Failed: <%= obj.name %></p>\n'
+ ' <p>Value: <%= obj.value %></p>\n'
+ ' <% } %>\n'
+ ' <% } %>\n'
+ ' <% } %>\n'
, strNoWith = ' <% for (var i = 0; i < locals.data.length; i++) { %>\n'
+ ' <% var objects = locals.data[i] %>\n'
+ ' <h1><%= objects.title %></h1>\n'
+ ' <% for (var j = 0; j < objects.length; j++) { %>\n'
+ ' <% var obj = objects[j] %>\n'
+ ' <% if (obj.passed) { %>\n'
+ ' <p>Passed: <%= obj.name %></p>\n'
+ ' <p>Value: <%= obj.value %></p>\n'
+ ' <% } else { %>\n'
+ ' <p>Failed: <%= obj.name %></p>\n'
+ ' <p>Value: <%= obj.value %></p>\n'
+ ' <% } %>\n'
+ ' <% } %>\n'
+ ' <% } %>\n'
, obj = { data: [
{
title: "Test title",
objects: [
{
name: 'this',
value: 21032,
passed: false
}, {
name: 'test',
value: 0,
passed: true
}
]
}, {
title: "Another data",
objects: [
{
name: 'that',
value: 1012021,
passed: true
}, {
name: 'tset',
value: -2,
passed: false
}
]
}
]}
var opts = {}
, fnEjs1 = ejs1.compile(str, opts)
, fnEjs2 = ejs2.compile(str, opts)
, fnNEjs2 = newEjs2.compile(str, opts)
opts._with = false
var fnEjs1NoWith = ejs1.compile(strNoWith, opts)
, fnEjs2NoWith = ejs2.compile(strNoWith, opts)
, fnNEjs2NoWith = newEjs2.compile(strNoWith, opts)
suite
.add('ejs v1 comp', function() {
ejs1.compile(str)
})
.add('ejs v2 orig comp', function() {
ejs2.compile(str)
})
.add('ejs v2 new comp', function() {
newEjs2.compile(str)
})
.add('ejs v1', function() {
fnEjs1(obj)
})
.add('ejs v1 !_with', function() {
fnEjs1NoWith(obj)
})
.add('ejs v2 orig', function() {
fnEjs2(obj)
})
.add('ejs v2 orig !_with', function() {
fnEjs2NoWith(obj)
})
.add('ejs v2 new', function() {
fnNEjs2(obj)
})
.add('ejs v2 new !_with', function() {
fnNEjs2NoWith(obj)
})
.on('cycle', function(event) {
console.log(String(event.target));
})
.run()
.run()

(Only the first run pasted here)

Higher is better.

Compilation

ejs v1 comp x 24,313 ops/sec ±0.31% (100 runs sampled)         
ejs v2 orig comp x 7,620 ops/sec ±0.72% (101 runs sampled)
ejs v2 new comp x 26,793 ops/sec ±1.44% (96 runs sampled)
    | v1   | v2 orig | v2 new |

--------|-----:|--------:|-------:| v1 | 100% | 319% | 90% | v2 orig | 31% | 100% | 28% | v2 new | 110% | 352% | 100% |

Template execution

ejs v1 x 255,559 ops/sec ±0.99% (94 runs sampled)
ejs v1 !_with x 711,009 ops/sec ±0.41% (101 runs sampled)
ejs v2 orig x 128,351 ops/sec ±0.62% (97 runs sampled)
ejs v2 orig !_with x 528,648 ops/sec ±0.63% (99 runs sampled)
ejs v2 new x 174,185 ops/sec ±0.45% (99 runs sampled)
ejs v2 new !_with x 666,686 ops/sec ±0.91% (101 runs sampled)

Fair comparisons are highlighted.

       |    v1    | v1 !_with|  v2 orig | v2 orig !_with |  v2 new  | v2 new !_with |

-----------|---:|---:|---:|---:|---:|---:| v1 | 100% | 36% | 199% | 48% | 146% | 38% | v1 !_with | 278% | 100% | 554% | 134% | 408% | 107% | v2o | 50% | 18% | 100% | 24% | 74% | 19% | v2o !_with | 207% | 74% | 411% | 100% | 303% | 79% | v2n | 68% | 24% | 136% | 33% | 100% | 26% | v2n !_with | 261% | 94% | 519% | 126% | 383% | 100% |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment