Skip to content

Commit

Permalink
perf: remove argument reassignments in View
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jun 20, 2015
1 parent 0634e7e commit a8a8564
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 26 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ unreleased
- perf: reduce try block size
- perf: remove bitwise operations
* perf: enable strict mode
* perf: remove argument reassignments in `View`
* perf: use saved reference to `http.STATUS_CODES`

4.12.4 / 2015-05-17
Expand Down
73 changes: 47 additions & 26 deletions lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

/**
* Module dependencies.
* @private
*/

var debug = require('debug')('express:view');
Expand All @@ -29,7 +30,8 @@ var join = path.join;
var resolve = path.resolve;

/**
* Expose `View`.
* Module exports.
* @public
*/

module.exports = View;
Expand All @@ -43,30 +45,51 @@ module.exports = View;
* - `engines` template engine require() cache
* - `root` root path for view lookup
*
* @param {String} name
* @param {Object} options
* @api private
* @param {string} name
* @param {object} options
* @public
*/

function View(name, options) {
options = options || {};
var opts = options || {};

this.defaultEngine = opts.defaultEngine;
this.ext = extname(name);
this.name = name;
this.root = options.root;
var engines = options.engines;
this.defaultEngine = options.defaultEngine;
var ext = this.ext = extname(name);
if (!ext && !this.defaultEngine) throw new Error('No default engine was specified and no extension was provided.');
if (!ext) name += (ext = this.ext = ('.' != this.defaultEngine[0] ? '.' : '') + this.defaultEngine);
this.engine = engines[ext] || (engines[ext] = require(ext.slice(1)).__express);
this.path = this.lookup(name);
this.root = opts.root;

if (!this.ext && !this.defaultEngine) {
throw new Error('No default engine was specified and no extension was provided.');
}

var fileName = name;

if (!this.ext) {
// get extension from default engine name
this.ext = this.defaultEngine[0] !== '.'
? '.' + this.defaultEngine
: this.defaultEngine;

fileName += this.ext;
}

if (!opts.engines[this.ext]) {
// load engine
opts.engines[this.ext] = require(this.ext.substr(1)).__express;
}

// store loaded engine
this.engine = opts.engines[this.ext];

// lookup path
this.path = this.lookup(fileName);
}

/**
* Lookup view by the given `name`
*
* @param {String} name
* @return {String}
* @api private
* @param {string} name
* @private
*/

View.prototype.lookup = function lookup(name) {
Expand All @@ -91,16 +114,16 @@ View.prototype.lookup = function lookup(name) {
};

/**
* Render with the given `options` and callback `fn(err, str)`.
* Render with the given options.
*
* @param {Object} options
* @param {Function} fn
* @api private
* @param {object} options
* @param {function} callback
* @private
*/

View.prototype.render = function render(options, fn) {
View.prototype.render = function render(options, callback) {
debug('render "%s"', this.path);
this.engine(this.path, options, fn);
this.engine(this.path, options, callback);
};

/**
Expand All @@ -113,12 +136,10 @@ View.prototype.render = function render(options, fn) {

View.prototype.resolve = function resolve(dir, file) {
var ext = this.ext;
var path;
var stat;

// <path>.<ext>
path = join(dir, file);
stat = tryStat(path);
var path = join(dir, file);
var stat = tryStat(path);

if (stat && stat.isFile()) {
return path;
Expand Down

0 comments on commit a8a8564

Please sign in to comment.