Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance #30

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ test-cov cov:

test-all: jshint test test-cov

benchmark: install
@NODE_ENV=test node --harmony ./benchmarks/index

autod: install
@./node_modules/.bin/autod -w
@$(MAKE) install
Expand Down
29 changes: 29 additions & 0 deletions benchmarks/context.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions benchmarks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict'

var fs = require('fs')
var Benchmark = require('benchmark')
var suite = new Benchmark.Suite()
var VelocityEngine = require('../index').Engine
var nunjucks = require('nunjucks')
var context = require('./context')
var velocityTemplStr = fs.readFileSync(__dirname + '/template-velocity.vm', {encoding: 'utf-8'})
var nunjucksTemplStr = fs.readFileSync(__dirname + '/template-nunjucks.html', {encoding: 'utf-8'})

var velocityEngine = new VelocityEngine({
template: velocityTemplStr
})

var velocityTempl = velocityEngine.compile()

var nunjucksTempl = nunjucks.compile(nunjucksTemplStr)

suite.add('velocity without precompiling', function() {
velocityEngine.render(context)
})

.add('nunjucks without precompiling', function(defer) {
nunjucks.renderString(nunjucksTemplStr, context)
})

.add('velocity with precompiling', function() {
velocityTempl(context)
})

.add('nunjucks with precompiling', function() {
nunjucksTempl.render(context)
})

// add listeners
.on('cycle', function(event) {
console.log(String(event.target))
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'))
process.exit(0)
})
// run async
.run({ 'async': true })
15 changes: 15 additions & 0 deletions benchmarks/template-nunjucks.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<h1>Search completed in {{completed_in}}s</h1>
<ul class="meta-info">
<li>Your search keyword is $query</li>
<li>Click <a href="{{refresh_url}}">Here</a> to refresh</li>
</ul>
<ul class="tweetSearchResultList">
{% for item in results %}
<li>
<img src="{{ item.profile_image_url }}" alt="{{ item.from_user_name }}"/>
<p>{{item.text}}</p>
<span>{{item.id}}</span>
<span>{{item.created_at}}</span>
</li>
{% endfor %}
</ul>
15 changes: 15 additions & 0 deletions benchmarks/template-velocity.vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<h1>Search completed in ${completed_in}s</h1>
<ul class="meta-info">
<li>Your search keyword is $query</li>
<li>Click <a href="$refresh_url">Here</a> to refresh</li>
</ul>
<ul class="tweetSearchResultList">
#foreach($item in $results)
<li>
<img src="$item.profile_image_url" alt="$item.from_user_name"/>
<p>$item.text</p>
<span>$item.id</span>
<span>$item.created_at</span>
</li>
#end
</ul>
14 changes: 5 additions & 9 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,22 +160,18 @@ exports.perfectContext = function(context) {
var p = path.resolve(context)
;delete require.cache[p]
context = require(p)
} else if (utilx.isObject(context)) {
// do nothing
} else if (utilx.isString(context)) {
} else if (typeof context === 'string') {
try {
context = eval('(' + context + ')')
} catch(e) {
logger.error('Illegal context:', context)
}
} else {
context = {}
context = exports.isObject(context) ? context : {}
}
return context
}






exports.isObject = function(obj) {
return obj && typeof obj === 'object' && !Array.isArray(obj)
}
7 changes: 2 additions & 5 deletions lib/data/data-direc.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = {
}

o = rightr.__value
if (utilx.isArray(o)) {
if (Array.isArray(o)) {
if (!node.body) return
if (!o[0]) o[0] = { __stats: STATS.UNCERTAIN }
leftr = {
Expand Down Expand Up @@ -97,7 +97,7 @@ module.exports = {
if (!common.isLiteralString(arg)) return

var relPath = arg.value
if (!utilx.isNonEmptyString(relPath))
if (!relPath)
this.throwError('Param of #parse is not a non-empty string.', arg.pos)

var fullPath = common.getFullPath(relPath, this.cfg.root)
Expand Down Expand Up @@ -219,6 +219,3 @@ module.exports = {
Stop: function(node) {},
Break: function(node) {}
}



34 changes: 18 additions & 16 deletions lib/data/data-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ var utilx = require('utilx')
var inspect = require('util').inspect
var STATS = require('./data-stats')
var logger = require('../logger')
var common = require('../common')


// expand raw data to intermediate format
function expand(data) {
var rt

if (utilx.isObject(data)) {
if (common.isObject(data)) {
rt = {}
Object.keys(data).forEach(function(k) {
// ignore return value derived from template
Expand All @@ -18,7 +19,7 @@ function expand(data) {
return rt
}

if (utilx.isArray(data)) {
if (Array.isArray(data)) {
rt = []
data.forEach(function(v, k) {
innerExpand(data, k, rt)
Expand All @@ -31,7 +32,7 @@ function innerExpand(from, k, to/*private params*/, k2) {
var v = from[k]
var n

if (utilx.isFunction(v)) {
if (typeof v === 'function') {
n = { __stats: STATS.CERTAIN_FUNC }
var rtKey = '__' + k + 'Return'

Expand All @@ -46,7 +47,8 @@ function innerExpand(from, k, to/*private params*/, k2) {
}

} else {
var goon = utilx.isObject(v) || utilx.isArray(v)
// object or array
var goon = typeof v === 'object'
n = {
__stats: STATS.CERTAIN,
__value: goon ? expand(v) : v
Expand All @@ -60,7 +62,7 @@ function innerExpand(from, k, to/*private params*/, k2) {
// restore intermediate data to normal format
function clean(data) {
var rt
if (utilx.isObject(data)) {
if (common.isObject(data)) {
rt = {}
Object.keys(data).forEach(function(k) {
var n = data[k]
Expand All @@ -77,7 +79,7 @@ function clean(data) {
return rt
}

if (utilx.isArray(data)) {
if (Array.isArray(data)) {
rt = []
data.forEach(function(n, k) {
innerClean(data, k, rt)
Expand All @@ -94,7 +96,8 @@ function innerClean(from, k, to/*private params*/, k2) {
var v = n.__value

if (stats === STATS.CERTAIN) {
if (utilx.isObject(v) || utilx.isArray(v)) {
// object or array
if (typeof v === 'object') {
v = clean(v)
}

Expand Down Expand Up @@ -123,7 +126,7 @@ function tostr(data /* private params*/, indent) {
var oldBr = '\n' + utilx.generateLine(indent - 2, ' ')
var br = oldBr + ' '

if (utilx.isObject(data)) {
if (common.isObject(data)) {
var keys = Object.keys(data)
var len = keys.length

Expand All @@ -138,7 +141,7 @@ function tostr(data /* private params*/, indent) {
return rt
}

if (utilx.isArray(data)) {
if (Array.isArray(data)) {
var len = data.length
var rt = '['
data.forEach(function(item, idx) {
Expand All @@ -149,11 +152,11 @@ function tostr(data /* private params*/, indent) {
return rt
}

if (utilx.isFunction(data)) {
if (typeof data === 'function') {
return data.toString()
}

if (utilx.isString(data)) {
if (typeof data === 'string') {
return inspect(data)
}

Expand All @@ -173,7 +176,7 @@ function dump(data/* private params*/, literal, indent) {
var head = isTop ? '' : '#if(' + literal + ')'
var tail = isTop ? '' : '#{else}undefined#end'

if (utilx.isObject(data)) {
if (common.isObject(data)) {
var keys = Object.keys(data)
var len = keys.length

Expand Down Expand Up @@ -206,7 +209,7 @@ function dump(data/* private params*/, literal, indent) {
return rt
}

if (utilx.isArray(data)) {
if (Array.isArray(data)) {
// NOTE
// if the itemLiteral is always the same
// $list1.list2 will cause an error
Expand All @@ -220,11 +223,11 @@ function dump(data/* private params*/, literal, indent) {
return rt
}

if (utilx.isFunction(data)) {
if (typeof data === 'function') {
return data.toString()
}

if (utilx.isString(data)) {
if (typeof data === 'string') {
if (data === '') {
return '\'' + literal + '\''
} else {
Expand Down Expand Up @@ -263,4 +266,3 @@ exports.expand = expand
exports.clean = clean
exports.tostr = tostr
exports.dump = dump

Loading