Skip to content

Commit daa978d

Browse files
authored
Merge pull request #15677 from Automattic/vkarpov15/gh-15672-2
perf: avoid getting all modified paths in update when checking if versionKey needs to be set
2 parents d030c29 + 4cf2669 commit daa978d

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed
Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict';
22

3-
const modifiedPaths = require('./modifiedPaths');
4-
53
/**
64
* Decorate the update with a version key, if necessary
75
* @api private
@@ -12,15 +10,26 @@ module.exports = function decorateUpdateWithVersionKey(update, options, versionK
1210
return;
1311
}
1412

15-
const updatedPaths = modifiedPaths(update);
16-
if (!updatedPaths[versionKey]) {
17-
if (options.overwrite) {
13+
if (options.overwrite) {
14+
if (!hasKey(update, versionKey)) {
1815
update[versionKey] = 0;
19-
} else {
20-
if (!update.$setOnInsert) {
21-
update.$setOnInsert = {};
22-
}
23-
update.$setOnInsert[versionKey] = 0;
2416
}
17+
} else if (
18+
!hasKey(update, versionKey) &&
19+
!hasKey(update?.$set, versionKey) &&
20+
!hasKey(update?.$inc, versionKey) &&
21+
!hasKey(update?.$setOnInsert, versionKey)
22+
) {
23+
if (!update.$setOnInsert) {
24+
update.$setOnInsert = {};
25+
}
26+
update.$setOnInsert[versionKey] = 0;
2527
}
2628
};
29+
30+
function hasKey(obj, key) {
31+
if (obj == null || typeof obj !== 'object') {
32+
return false;
33+
}
34+
return Object.prototype.hasOwnProperty.call(obj, key);
35+
}

0 commit comments

Comments
 (0)