Skip to content

Commit

Permalink
perf(document): avoid unnecessarily creating new options object on ev…
Browse files Browse the repository at this point in the history
…ery `$set`

Re: #11541
  • Loading branch information
vkarpov15 committed Jun 12, 2022
1 parent f1c5412 commit 3dc732f
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,11 @@ Document.prototype.$__buildDoc = function(obj, fields, skipId, exclude, hasInclu
for (let i = 0; i < len; ++i) {
const piece = path[i];

curPath += (!curPath.length ? '' : '.') + piece;
if (!curPath.length) {
curPath = piece;
} else {
curPath += '.' + piece;
}

// support excluding intermediary levels
if (exclude === true) {
Expand Down Expand Up @@ -1044,8 +1048,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
type = undefined;
}

options = options || {};
const merge = options.merge;
const merge = options && options.merge;
const adhoc = type && type !== true;
const constructing = type === true;
let adhocs;
Expand All @@ -1055,7 +1058,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
let key;
let prefix;

const strict = 'strict' in options
const strict = options && 'strict' in options
? options.strict
: this.$__.strictMode;

Expand Down Expand Up @@ -1086,7 +1089,7 @@ Document.prototype.$set = function $set(path, val, type, options) {

// `_skipMinimizeTopLevel` is because we may have deleted the top-level
// nested key to ensure key order.
const _skipMinimizeTopLevel = options._skipMinimizeTopLevel || false;
const _skipMinimizeTopLevel = options && options._skipMinimizeTopLevel || false;
if (len === 0 && _skipMinimizeTopLevel) {
delete options._skipMinimizeTopLevel;
if (val) {
Expand Down Expand Up @@ -1564,7 +1567,7 @@ Document.prototype.set = Document.prototype.$set;
*/

Document.prototype.$__shouldModify = function(pathToMark, path, options, constructing, parts, schema, val, priorVal) {
if (options._skipMarkModified) {
if (options && options._skipMarkModified) {
return false;
}
if (this.$isNew) {
Expand Down

0 comments on commit 3dc732f

Please sign in to comment.