Skip to content

Commit 537032d

Browse files
authored
Merge pull request Automattic#15022 from Automattic/vkarpov15/Automatticgh-14906
perf: make a few micro-optimizations to help speed up findOne()
2 parents 9df19b6 + e40a090 commit 537032d

File tree

4 files changed

+16
-20
lines changed

4 files changed

+16
-20
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 & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const castUpdate = require('./helpers/query/castUpdate');
2222
const clone = require('./helpers/clone');
2323
const getDiscriminatorByValue = require('./helpers/discriminator/getDiscriminatorByValue');
2424
const helpers = require('./queryHelpers');
25-
const immediate = require('./helpers/immediate');
2625
const internalToObjectOptions = require('./options').internalToObjectOptions;
2726
const isExclusive = require('./helpers/projection/isExclusive');
2827
const isInclusive = require('./helpers/projection/isInclusive');
@@ -2248,6 +2247,9 @@ Query.prototype.error = function error(err) {
22482247
*/
22492248

22502249
Query.prototype._unsetCastError = function _unsetCastError() {
2250+
if (this._error == null) {
2251+
return;
2252+
}
22512253
if (this._error != null && !(this._error instanceof CastError)) {
22522254
return;
22532255
}
@@ -2291,9 +2293,9 @@ Query.prototype.mongooseOptions = function(v) {
22912293

22922294
Query.prototype._castConditions = function() {
22932295
let sanitizeFilterOpt = undefined;
2294-
if (this.model != null && utils.hasUserDefinedProperty(this.model.db.options, 'sanitizeFilter')) {
2296+
if (this.model?.db.options?.sanitizeFilter != null) {
22952297
sanitizeFilterOpt = this.model.db.options.sanitizeFilter;
2296-
} else if (this.model != null && utils.hasUserDefinedProperty(this.model.base.options, 'sanitizeFilter')) {
2298+
} else if (this.model?.base.options?.sanitizeFilter != null) {
22972299
sanitizeFilterOpt = this.model.base.options.sanitizeFilter;
22982300
} else {
22992301
sanitizeFilterOpt = this._mongooseOptions.sanitizeFilter;
@@ -2536,13 +2538,12 @@ Query.prototype.collation = function(value) {
25362538
* @api private
25372539
*/
25382540

2539-
Query.prototype._completeOne = function(doc, res, callback) {
2541+
Query.prototype._completeOne = function(doc, res, projection, callback) {
25402542
if (!doc && !this.options.includeResultMetadata) {
25412543
return callback(null, null);
25422544
}
25432545

25442546
const model = this.model;
2545-
const projection = clone(this._fields);
25462547
const userProvidedFields = this._userProvidedFields || {};
25472548
// `populate`, `lean`
25482549
const mongooseOptions = this._mongooseOptions;
@@ -2643,7 +2644,7 @@ Query.prototype._findOne = async function _findOne() {
26432644
// don't pass in the conditions because we already merged them in
26442645
const doc = await this.mongooseCollection.findOne(this._conditions, options);
26452646
return new Promise((resolve, reject) => {
2646-
this._completeOne(doc, null, (err, res) => {
2647+
this._completeOne(doc, null, options.projection, (err, res) => {
26472648
if (err) {
26482649
return reject(err);
26492650
}
@@ -3238,7 +3239,7 @@ function completeOne(model, doc, res, options, fields, userProvidedFields, pop,
32383239

32393240
function _init(err, casted) {
32403241
if (err) {
3241-
return immediate(() => callback(err));
3242+
return callback(err);
32423243
}
32433244

32443245

@@ -3251,12 +3252,12 @@ function completeOne(model, doc, res, options, fields, userProvidedFields, pop,
32513252
} else {
32523253
res.value = null;
32533254
}
3254-
return immediate(() => callback(null, res));
3255+
return callback(null, res);
32553256
}
32563257
if (options.session != null) {
32573258
casted.$session(options.session);
32583259
}
3259-
immediate(() => callback(null, casted));
3260+
callback(null, casted);
32603261
}
32613262
}
32623263

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

34673468
return new Promise((resolve, reject) => {
3468-
this._completeOne(doc, res, (err, res) => {
3469+
this._completeOne(doc, res, options.projection, (err, res) => {
34693470
if (err) {
34703471
return reject(err);
34713472
}
@@ -3561,7 +3562,7 @@ Query.prototype._findOneAndDelete = async function _findOneAndDelete() {
35613562
const doc = !includeResultMetadata ? res : res.value;
35623563

35633564
return new Promise((resolve, reject) => {
3564-
this._completeOne(doc, res, (err, res) => {
3565+
this._completeOne(doc, res, options.projection, (err, res) => {
35653566
if (err) {
35663567
return reject(err);
35673568
}
@@ -3715,7 +3716,7 @@ Query.prototype._findOneAndReplace = async function _findOneAndReplace() {
37153716

37163717
const doc = !includeResultMetadata ? res : res.value;
37173718
return new Promise((resolve, reject) => {
3718-
this._completeOne(doc, res, (err, res) => {
3719+
this._completeOne(doc, res, options.projection, (err, res) => {
37193720
if (err) {
37203721
return reject(err);
37213722
}

0 commit comments

Comments
 (0)