我一直在尝试为MVC框架创建一个视图模块。
我有2个版本的代码。一个同步和一个异步。
我知道我应该使用Async版本。但是,每当我通过AB进行负载测试时,我都会从Sync版本中获得更好的结果。
我做错了吗?这是我正在测试的代码示例
同步版本:
var fs = require('fs'),
path = require('path'),
dot = require('dot');
Step = require('../libraries/step.js');
var view = function(){};
view.prototype = {
renderView : function(source, data, layout, callback)
{
var self = this;
if (typeof callback !== 'function') throw ViewCallbackException;
layout = layout ? layout : 'default';
(function(sources, datas, layouts, callbacks)
{
content = fs.readFileSync('./views/layouts/' + layouts + '/index.html', 'utf-8');
callbacks(null, content);
})(source, data, layout, callback);
}
}
异步版本:
var fs = require('fs'),
path = require('path'),
dot = require('dot');
Step = require('../libraries/step.js');
var view = function(){};
view.prototype = {
renderView : function(source, data, layout, callback)
{
var self = this;
if (typeof callback !== 'function') throw ViewCallbackException;
layout = layout ? layout : 'default';
(function(sources, datas, layouts, callbacks)
{
Step(
function()
{
fs.readFile('./views/layouts/' + layouts + '/index.html', 'utf-8', this);
},
callbacks
);
})(source, data, layout, callback);
}
}