Skip to content

Commit 31104f8

Browse files
committed
perf: make a few micro-optimizations to help speed up findOne()
Re: Automattic#14906
1 parent 9df19b6 commit 31104f8

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

lib/helpers/isBsonType.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
function isBsonType(obj, typename) {
99
return (
10-
typeof obj === 'object' &&
11-
obj !== null &&
10+
obj != null &&
1211
obj._bsontype === typename
1312
);
1413
}

lib/helpers/schema/applyReadConcern.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict';
22

3-
const get = require('../get');
4-
53
module.exports = function applyReadConcern(schema, options) {
64
if (options.readConcern !== undefined) {
75
return;
@@ -15,7 +13,7 @@ module.exports = function applyReadConcern(schema, options) {
1513
return;
1614
}
1715

18-
const level = get(schema, 'options.readConcern.level', null);
16+
const level = schema.options?.readConcern?.level;
1917
if (level != null) {
2018
options.readConcern = { level };
2119
}

lib/helpers/schema/applyWriteConcern.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict';
22

3-
const get = require('../get');
4-
53
module.exports = function applyWriteConcern(schema, options) {
64
if (options.writeConcern != null) {
75
return;
@@ -12,7 +10,7 @@ module.exports = function applyWriteConcern(schema, options) {
1210
if (options && options.session && options.session.transaction) {
1311
return;
1412
}
15-
const writeConcern = get(schema, 'options.writeConcern', {});
13+
const writeConcern = schema.options.writeConcern ?? {};
1614
if (Object.keys(writeConcern).length != 0) {
1715
options.writeConcern = {};
1816
if (!('w' in options) && writeConcern.w != null) {

lib/query.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2248,6 +2248,9 @@ Query.prototype.error = function error(err) {
22482248
*/
22492249

22502250
Query.prototype._unsetCastError = function _unsetCastError() {
2251+
if (this._error == null) {
2252+
return;
2253+
}
22512254
if (this._error != null && !(this._error instanceof CastError)) {
22522255
return;
22532256
}
@@ -2291,9 +2294,9 @@ Query.prototype.mongooseOptions = function(v) {
22912294

22922295
Query.prototype._castConditions = function() {
22932296
let sanitizeFilterOpt = undefined;
2294-
if (this.model != null && utils.hasUserDefinedProperty(this.model.db.options, 'sanitizeFilter')) {
2297+
if (this.model != null && this.model.db.options?.sanitizeFilter != null) {
22952298
sanitizeFilterOpt = this.model.db.options.sanitizeFilter;
2296-
} else if (this.model != null && utils.hasUserDefinedProperty(this.model.base.options, 'sanitizeFilter')) {
2299+
} else if (this.model != null && this.model.base.options?.sanitizeFilter != null) {
22972300
sanitizeFilterOpt = this.model.base.options.sanitizeFilter;
22982301
} else {
22992302
sanitizeFilterOpt = this._mongooseOptions.sanitizeFilter;
@@ -2536,13 +2539,12 @@ Query.prototype.collation = function(value) {
25362539
* @api private
25372540
*/
25382541

2539-
Query.prototype._completeOne = function(doc, res, callback) {
2542+
Query.prototype._completeOne = function(doc, res, projection, callback) {
25402543
if (!doc && !this.options.includeResultMetadata) {
25412544
return callback(null, null);
25422545
}
25432546

25442547
const model = this.model;
2545-
const projection = clone(this._fields);
25462548
const userProvidedFields = this._userProvidedFields || {};
25472549
// `populate`, `lean`
25482550
const mongooseOptions = this._mongooseOptions;
@@ -2643,7 +2645,7 @@ Query.prototype._findOne = async function _findOne() {
26432645
// don't pass in the conditions because we already merged them in
26442646
const doc = await this.mongooseCollection.findOne(this._conditions, options);
26452647
return new Promise((resolve, reject) => {
2646-
this._completeOne(doc, null, (err, res) => {
2648+
this._completeOne(doc, null, options.projection, (err, res) => {
26472649
if (err) {
26482650
return reject(err);
26492651
}
@@ -3238,7 +3240,7 @@ function completeOne(model, doc, res, options, fields, userProvidedFields, pop,
32383240

32393241
function _init(err, casted) {
32403242
if (err) {
3241-
return immediate(() => callback(err));
3243+
return callback(err);
32423244
}
32433245

32443246

@@ -3251,12 +3253,12 @@ function completeOne(model, doc, res, options, fields, userProvidedFields, pop,
32513253
} else {
32523254
res.value = null;
32533255
}
3254-
return immediate(() => callback(null, res));
3256+
return callback(null, res);
32553257
}
32563258
if (options.session != null) {
32573259
casted.$session(options.session);
32583260
}
3259-
immediate(() => callback(null, casted));
3261+
callback(null, casted);
32603262
}
32613263
}
32623264

@@ -3465,7 +3467,7 @@ Query.prototype._findOneAndUpdate = async function _findOneAndUpdate() {
34653467
const doc = !options.includeResultMetadata ? res : res.value;
34663468

34673469
return new Promise((resolve, reject) => {
3468-
this._completeOne(doc, res, (err, res) => {
3470+
this._completeOne(doc, res, options.projection, (err, res) => {
34693471
if (err) {
34703472
return reject(err);
34713473
}
@@ -3561,7 +3563,7 @@ Query.prototype._findOneAndDelete = async function _findOneAndDelete() {
35613563
const doc = !includeResultMetadata ? res : res.value;
35623564

35633565
return new Promise((resolve, reject) => {
3564-
this._completeOne(doc, res, (err, res) => {
3566+
this._completeOne(doc, res, options.projection, (err, res) => {
35653567
if (err) {
35663568
return reject(err);
35673569
}
@@ -3715,7 +3717,7 @@ Query.prototype._findOneAndReplace = async function _findOneAndReplace() {
37153717

37163718
const doc = !includeResultMetadata ? res : res.value;
37173719
return new Promise((resolve, reject) => {
3718-
this._completeOne(doc, res, (err, res) => {
3720+
this._completeOne(doc, res, options.projection, (err, res) => {
37193721
if (err) {
37203722
return reject(err);
37213723
}

0 commit comments

Comments
 (0)