Skip to content

Commit

Permalink
perf: don't create wrapper function if no hooks specified
Browse files Browse the repository at this point in the history
Fix #6126
  • Loading branch information
vkarpov15 committed Feb 22, 2018
1 parent 360dac5 commit 6a2ed04
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 28 deletions.
25 changes: 13 additions & 12 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1521,8 +1521,8 @@ function _getPathsToValidate(doc) {
*/

Document.prototype.$__validate = function(callback) {
var _this = this;
var _complete = function() {
const _this = this;
const _complete = function() {
var err = _this.$__.validationError;
_this.$__.validationError = undefined;
_this.emit('validate', _this);
Expand All @@ -1540,11 +1540,11 @@ Document.prototype.$__validate = function(callback) {
};

// only validate required fields when necessary
var paths = _getPathsToValidate(this);
const paths = _getPathsToValidate(this);

if (paths.length === 0) {
return process.nextTick(function() {
var error = _complete();
const error = _complete();
if (error) {
return _this.schema.s.hooks.execPost('validate:error', _this, [ _this], { error: error }, function(error) {
callback(error);
Expand All @@ -1554,11 +1554,11 @@ Document.prototype.$__validate = function(callback) {
});
}

var validated = {};
var total = 0;
const validated = {};
let total = 0;

var complete = function() {
var error = _complete();
const error = _complete();
if (error) {
return _this.schema.s.hooks.execPost('validate:error', _this, [ _this], { error: error }, function(error) {
callback(error);
Expand All @@ -1576,7 +1576,7 @@ Document.prototype.$__validate = function(callback) {
total++;

process.nextTick(function() {
var p = _this.schema.path(path);
const p = _this.schema.path(path);
if (!p) {
return --total || complete();
}
Expand All @@ -1587,10 +1587,11 @@ Document.prototype.$__validate = function(callback) {
return;
}

var val = _this.getValue(path);
var scope = path in _this.$__.pathsToScopes ?
const val = _this.getValue(path);
const scope = path in _this.$__.pathsToScopes ?
_this.$__.pathsToScopes[path] :
_this;

p.doValidate(val, function(err) {
if (err) {
_this.invalidate(path, err, undefined, true);
Expand All @@ -1600,8 +1601,8 @@ Document.prototype.$__validate = function(callback) {
});
};

var numPaths = paths.length;
for (var i = 0; i < numPaths; ++i) {
const numPaths = paths.length;
for (let i = 0; i < numPaths; ++i) {
validatePath(paths[i]);
}
};
Expand Down
10 changes: 5 additions & 5 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -1442,14 +1442,14 @@ function completeMany(model, docs, fields, userProvidedFields, pop, callback) {
var opts = pop ? { populated: pop } : undefined;
var error = null;
function init(_error) {
if (_error != null) {
error = error || _error;
}
if (error != null) {
--count || callback(error);
return;
}
if (_error != null) {
error = _error;
return callback(error);
}
--count || callback(null, arr);
--count || callback(error, arr);
}
for (var i = 0; i < len; ++i) {
arr[i] = helpers.createModel(model, docs[i], fields, userProvidedFields);
Expand Down
8 changes: 1 addition & 7 deletions lib/schema/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,7 @@ DocumentArray.prototype.doValidate = function(array, fn, scope, options) {
undefined, i);
}

// HACK: use $__original_validate to avoid promises so bluebird doesn't
// complain
if (doc.$__original_validate) {
doc.$__original_validate({__noPromise: true}, callback);
} else {
doc.validate({__noPromise: true}, callback);
}
doc.$__validate(callback);
}
}, scope);
};
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"dependencies": {
"async": "2.1.4",
"bson": "~1.0.4",
"kareem": "2.0.4",
"kareem": "2.0.5",
"lodash.get": "4.4.2",
"mongodb": "3.0.2",
"mongoose-legacy-pluralize": "1.0.2",
Expand Down

0 comments on commit 6a2ed04

Please sign in to comment.