Created
November 12, 2014 03:54
-
-
Save poying/f07aea5cb1e9f3e2d025 to your computer and use it in GitHub Desktop.
benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var co = require('co'); | |
var prettyHrtime = require('pretty-hrtime'); | |
var jsdom = require('./jsdom'); | |
var templateEngine = require('./template-engine'); | |
var times = 100; | |
function *bench(fn) { | |
var i = 0; | |
var start = process.hrtime(); | |
while (i < times) { | |
i++; | |
yield fn(); | |
} | |
console.log(prettyHrtime(process.hrtime(start), { verbose: true })); | |
} | |
co(function *() { | |
yield bench(jsdom); | |
yield bench(templateEngine); | |
console.log('-------------------------'); | |
yield bench(jsdom); | |
yield bench(templateEngine); | |
console.log('-------------------------'); | |
yield bench(jsdom); | |
yield bench(templateEngine); | |
console.log('-------------------------'); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var domify = require('component/domify'); | |
var template = require('./template.html'); | |
module.exports = Component; | |
function Component(data) { | |
data || (data = {}); | |
var el = domify(template); | |
this.el = el; | |
el.querySelector('.title').textContent = data.title; | |
el.querySelector('.content').textContent = data.content; | |
} | |
Component.prototype.toHTML = function () { | |
return this.el.outerHTML; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<h3 class="title"></h3> | |
<div class="content"></div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var requireDuo = require('require-duo'); | |
module.exports = function () { | |
return requireDuo(__dirname) | |
.entry('jsdom-component/index.js') | |
.script(function (View) { | |
var data = { | |
title: 'abc', | |
content: 'lol' | |
}; | |
return (new View(data)).toHTML(); | |
}) | |
.exec(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var template, domify, minstache; | |
var isBrowser = true; | |
try { | |
template = require('./template.html'); | |
domify = require('component/domify'); | |
minstache = require('visionmedia/minstache'); | |
} catch (e) { | |
var fs = require('fs'); | |
var path = require('path'); | |
minstache = require('minstache'); | |
template = fs.readFileSync(path.join(__dirname, 'template.html'), 'utf8'); | |
isBrowser = false; | |
} | |
module.exports = Component; | |
function Component(data) { | |
data || (data = {}); | |
this.html = minstache(template, data); | |
if (isBrowser) { | |
this.el = domify(this.html); | |
} | |
} | |
Component.prototype.toHTML = function () { | |
return isBrowser ? this.el.outerHTML : this.html; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<h3 class="title">{{title}}</h3> | |
<div class="content">{{content}}</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var View = require('./template-engine-component'); | |
module.exports = function *() { | |
var data = { | |
title: 'abc', | |
content: 'lol' | |
}; | |
var view = new View(data); | |
return view.toHTML(); | |
}; |
Author
poying
commented
Nov 12, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment